How do I integrate C++11 into Android?
-
There's no need to create a new question, simply answer your original question.– LittleBobbyTables - Au RevoirCommented Mar 25, 2013 at 13:41
-
9Since the original question was about using "unordered_map", I believed it would be interesting to have a question about integrating C++11 on Android because it took me some time and it is slightly different. Glad to get a down vote on a wiki question, by the way ;-).– JonasVautherinCommented Mar 25, 2013 at 13:45
-
1gotcha. Guess someone didn't like the question :|– LittleBobbyTables - Au RevoirCommented Mar 25, 2013 at 13:46
4 Answers
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
-
2Adding
LOCAL_CPPFLAGS := -stc=c++11
to myAndroid.mk
also worked for me.– GregCommented May 21, 2018 at 20:39
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)
-
-
I'm having some problems with this. I use windows and I compile via cmd. I added LOCAL_CFLAGS := -D__GXX_EXPERIMENTAL_CXX0X__ -std=gnu++11 in my make file, but when I run ndk-build command, I get error in places where unordered_map is used ('unordered_map' does not name a type). Code works in linux and windows ports of my program. Any ideas what might be wrong? NDK I use is the newest available (downloaded today).– SMGhostCommented Mar 31, 2013 at 10:18
-
3If you have more than one module and you want to enable the flag for all the files you can add the flag to APP_CPPFLAGS variable in the Application.mk file.– haraCommented Oct 15, 2013 at 12:35
-
2Please see my answer below for a more up-to-date version for the now standardized C++11 STL. Commented Jan 27, 2014 at 17:00
-
What happens with STLport? It appears to be one of the C+ libraries that pretends to be C++11 by defining
__cplusplus >= 201103L
. STLport is missing<mutex>
,<atomics>
, etc.– jwwCommented Jul 17, 2016 at 22:15
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")
}
-
-
Hey @Seth, many things could go wrong, Have you make sure you have used the correct version of gradle plugin and the right version of the gradle itself? This is a great guild if you haven't read it. tools.android.com/tech-docs/new-build-system/…– YuchenCommented Feb 12, 2017 at 21:39
-
Thanks. But sadly I have read every guide out there. lol. I just used
pthread
instead ofthread
– SethCommented Feb 12, 2017 at 22:08
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")
}
}
}