1

I have the latest version of the 64bit NDK (r10c), and the latest version of Android Studio I can download (0.8.14).

I am making a number of JNI calls to use String, Vector, Atomic, etc. But I can not figure out how to use thread and mutex.

Both of them give me the same error

Error:(92, 5) error: 'thread' is not a member of 'std'
Error:(93, 5) error: 'mutex' is not a member of 'std'

I'm sure that the NDK is using 4.9 of the gnu-libstdc++. If I put in a #error in the file I see my error and compilation stops. It appears that I'm not missing any defines since I can put the #error inside the class and see it.

Here is the ndk config in my build.gradle

ndk {
    moduleName "myLib"
    ldLibs "log"
    stl "gnustl_shared"
    cFlags "-std=c++11 -frtti -fexceptions -pthread"
}

The -frtti and -pthread seem to make no difference. I have also tried stl of gnustl_shared as well as gnustl_static, no difference.

Brian S
  • 3,096
  • 37
  • 55

1 Answers1

3

By default, NDK still uses GCC 4.6 which has crippled support for C++11. You need the grade equivalent for setting NDK_TOOLCHAIN_VERSION:=4.9 in Application.mk. You can find some answers here: how to specify NDK_TOOLCHAIN_VERSION in gradle file for android ndk build, but unfortunately the bottom line is that today you have to disable automatic ndk-build call by setting jni.srcDirs to empty and use the Android.mk and Application.mk files the old way.

So, if in your jni directory, there are files file1.cpp and file2.cpp, you will need the following Android.mk

LOCAL_PATH            := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE          := myLib
LOCAL_SRC_FILES       := file1.cpp file2.cpp
LOCAL_LDLIBS          := -llog
LOCAL_CFLAGS          := -std=c++11 -frtti -fexceptions -pthread

... and Application.mk

APP_ABI               := armeabi-v7a
APP_STL               := gnustl_shared
NDK_TOOLCHAIN_VERSION := 4.9
Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Are you sure it's 4.6 by default? I am pretty sure it's using 4.9 because when I edit the thread file in the 4.9 folder of the NDK I get build errors. The article you cited, also states at the bottom that 64bit uses 4.9 by default now. – Brian S Nov 08 '14 at 21:34
  • .. but you are not building for `APP_ABI=arm64-v8a`, are you? – Alex Cohn Nov 09 '14 at 06:56
  • No, but isn't it telling that if I edit the thread include file in 4.9 and the #error shows that I have determined which file is being included? That comment was unclear to me if they meant the 64 bit version of the ndk, or a 64 bit build. – Brian S Nov 09 '14 at 18:07
  • It's super easy to check. Run ndk-build from command line, and you will see the difference, or you won't. – Alex Cohn Nov 09 '14 at 19:20
  • I'm not sure I follow. What do you want me to do with a ndk build from the command line? – Brian S Nov 09 '14 at 23:10
  • If you think that your build fails even though gcc 4.9 compiler is running, you can prove it by running this compiler (and 4.6, to compare) with ndk-build from command line. Or, use one of the _[solutions](http://stackoverflow.com/questions/16667903/android-studio-gradle-and-ndk)_ that let you run `ndk-build` as a step of your gradle build. – Alex Cohn Nov 10 '14 at 07:40
  • Do you have any tips on how to do the command line build? Or should I go with the initial link you sent? I edited the 4.6 version of thread, and I see an error in there now, so it appears that it's trying to compile against both? – Brian S Nov 10 '14 at 14:39
  • You don't need any link, simply put `Android.mk` and `Application.mk` files in jni directory, and run `ndk-build` from command line. – Alex Cohn Nov 10 '14 at 16:25
  • What do I need to have in the Android.mk and Application.mk files? I have not done jni in Eclipse, so I'm learning both that and Gradle. I can run ndk-build '-C' filePath('{Full path to src dir?}') – Brian S Nov 10 '14 at 17:06
  • `ndk-build -C "full path to src dir"` exactly. Check that `full_path_to_src_dir/jni/Android.mk` and `full_path_to_src_dir/jni/Application.mk` are there. You don't need `filePath()`. – Alex Cohn Nov 10 '14 at 17:35
  • But how do I generate the two mk files? – Brian S Nov 10 '14 at 17:37