Android NDK r9b and compiling C++11
android, android-ndk, c++11
Solution
I'm sorry, the following should have been a comment, not answer - because I don't know what's wrong in your code, but here is what you can do to figure out yourself:
Here's my minimal Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := HelloJni.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_CPPFLAGS := -std=c++11
NDK_TOOLCHAIN_VERSION=4.8
APP_STL=gnustl_static
And here is the minimal HelloJni.cpp
#include <jni.h>
#include <thread>
void doSomeWork( void )
{
__android_log_print(ANDROID_LOG_DEBUG, "HelloJni", "hello from thread...");
return;
}
extern "C"
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
std::thread t( doSomeWork );
t.join();
return env->NewStringUTF("Hello from JNI !");
}
It builds clean in r9b on my Mac. One thing to check: run `ndk-build V=1` and make sure that the link step is similar to
~/android-ndk-r9b/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libhello-jni.so -shared --sysroot=~/android-ndk-r9b/platforms/android-17/arch-arm ./obj/local/armeabi/objs-debug/hello-jni/HelloJni.o ~/android-ndk-r9b/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/libgnustl_static.a -lgcc -no-canonical-prefixes -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -L~/android-ndk-r9b/platforms/android-17/arch-arm/usr/lib -llog -lc -lm -o ./obj/local/armeabi/libhello-jni.so
and check output of command
~/android-ndk-r9b/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86/bin/arm-linux-androideabi-nm -C ~/android-ndk-r9b/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/libgnustl_static.a | grep std::thread
Here is what I get:
00000000 T std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>)
00000000 T std::thread::hardware_concurrency()
00000000 T std::thread::join()
00000000 T std::thread::detach()
Problem
I'm trying to compile some C++11 code using NDK r9b, however no matter what I try it doesn't work. Compiling without C++11 features works fine. Here is my Application.mk: ``` NDK_TOOLCHAIN_VERSION := 4.8 APP_STL := gnustl_static ``` Here is my Android.mk: ``` LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) FILE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp) LOCAL_CPPFLAGS := -std=c++11 -pthread -frtti -fexceptions LOCAL_MODULE := RAGEAndroid LOCAL_SRC_FILES := jni.c $(FILE_LIST:$(LOCAL_PATH)/%=%) LOCAL_LDLIBS := -llog -lm -landroid -lGLESv3 include $(BUILD_SHARED_LIBRARY) ``` I have tried setting the following too without luck: ``` LOCAL_CFLAGS := -D__GXX_EXPERIMENTAL_CXX0X__ LOCAL_CPPFLAGS := -std=gnu++11 -pthread -frtti -fexceptions ``` I have ensured that Eclipse has the following paths in C++ general->Paths and Symbols ``` (ndk path)/sources/cxx-stl/gnu-libstdc++/4.8/include (ndk path)/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/include ``` I have one C file (jni.c) and a couple of test cpp/hpp. Everything compiles fine without any C++11 features, and including something like <thread> or <memory> doesn't cause any complaints, however when actually creating a std::thread object I get "Type 'std::thread' could not be resolved". This also happens with other features I am trying to use (std::unique_ptr, std::shared_ptr, std::move() e.t.c). I've read many topics on getting C++11 to compile but nothing seems to work, I believe I've missed something but can't figure out what it is. Any help would be appreciated! EDIT: If I print __cplusplus it shows 201103L, so it looks like it's using the correct version. EDIT 2: std::atomic seems to work fine.