75

How do I integrate C++11 into Android?

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95

4 Answers4

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.

Community
  • 1
  • 1
scorpiodawg
  • 5,612
  • 3
  • 42
  • 62
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)

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
  • @yakk: that usually requires 2 days to pass. – moswald Mar 25 '13 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 Mar 31 '13 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 Oct 15 '13 at 12:35
  • 2
    Please see my answer below for a more up-to-date version for the now standardized C++11 STL. – scorpiodawg Jan 27 '14 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 ``, ``, etc. – jww Jul 17 '16 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")
}
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • This does not work. It tells me cppFlags cannot be found. – Seth Feb 12 '17 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. http://tools.android.com/tech-docs/new-build-system/gradle-experimental – Yuchen Feb 12 '17 at 21:39
  • Thanks. But sadly I have read every guide out there. lol. I just used `pthread` instead of `thread` – Seth Feb 12 '17 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")
        }
    }
}