Enable C++11 support on Android

AndroidC++C++11

Android Problem Overview


How do I integrate C++11 into Android?

Android Solutions


Solution 1 - Android

It appears the main answer here includes experimental support for C++11, and C++11 is not experimental anymore.

If you're using command line NDK support (I use IDEA community edition 13 for the Java stuff), then this is what I had to put in my jni/Application.mk to get C++11 support with API 19 (on OSX ML):

NDK_TOOLCHAIN_VERSION := 4.8
# APP_STL := stlport_shared  --> does not seem to contain C++11 features
APP_STL := gnustl_shared

# Enable c++11 extentions in source code
APP_CPPFLAGS += -std=c++11

Derived from here and here.

Solution 2 - Android

First of all, you will need to ensure that your toolchain is "Cross GCC". Whilst it was the default on my Linux, it was not on my MacOSX Lion.

In order to do this, go to Project Properties > C/C++ Build > Tool Chain Editor. "Current toolchain" should be set to "Cross GCC". You might need to uncheck the box "Display compatible toolchains only".

Then, add an option to LOCAL_CFLAGS in Android.mk:

LOCAL_CFLAGS := -std=gnu++11

We now need to inform Eclipse about where to find the corresponding new symbols (e.g. "std::unordered_map"). Go to Right Click on "jni" > Properties > C/C++ General -> Paths and Symbols -> Symbols -> GNU C++, and add the following symbol (by clicking "Add..."):

Name: __GXX_EXPERIMENTAL_CXX0X__
Value:

(i.e. let "Value" empty)

Solution 3 - Android

You can also set this in your build.gradle file if you are using the gradle-experimental-plugin:

android.ndk {
    moduleName = "hello-android-jni"
    stl = "stlport_shared"
    cppFlags.add("-std=c++11")
}

Solution 4 - Android

With the latest gradle-experimental-plugin 0.8.0-alpha4 add to your app/build.gradle:

model {
    android {
        ndk {
            moduleName "native"
            CFlags.add("-std=c11") // Enable C11 support
            ldLibs.add("log")
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonasVautherinView Question on Stackoverflow
Solution 1 - AndroidscorpiodawgView Answer on Stackoverflow
Solution 2 - AndroidJonasVautherinView Answer on Stackoverflow
Solution 3 - AndroidYuchenView Answer on Stackoverflow
Solution 4 - AndroidDzmitry PlashchynskiView Answer on Stackoverflow