75

How do I integrate C++11 into Android?

3
  • There's no need to create a new question, simply answer your original question. Commented Mar 25, 2013 at 13:41
  • 9
    Since 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 ;-). Commented Mar 25, 2013 at 13:45
  • 1
    gotcha. Guess someone didn't like the question :| Commented Mar 25, 2013 at 13:46

4 Answers 4

70

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.

1
  • 2
    Adding LOCAL_CPPFLAGS := -stc=c++11 to my Android.mk also worked for me.
    – Greg
    Commented May 21, 2018 at 20:39
37

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)

5
  • @yakk: that usually requires 2 days to pass.
    – moswald
    Commented Mar 25, 2013 at 21:53
  • 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).
    – SMGhost
    Commented Mar 31, 2013 at 10:18
  • 3
    If 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.
    – hara
    Commented Oct 15, 2013 at 12:35
  • 2
    Please 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.
    – jww
    Commented Jul 17, 2016 at 22:15
7

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")
}
3
  • This does not work. It tells me cppFlags cannot be found.
    – Seth
    Commented Feb 12, 2017 at 19:37
  • 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/…
    – Yuchen
    Commented Feb 12, 2017 at 21:39
  • Thanks. But sadly I have read every guide out there. lol. I just used pthread instead of thread
    – Seth
    Commented Feb 12, 2017 at 22:08
1

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")
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.