Android NDK r9b and C ++ 11 compilation

I am trying to compile C ++ 11 code using the NDK r9b, however no matter what I tried, it does not 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 also tried installing the following without any luck:

 LOCAL_CFLAGS := -D__GXX_EXPERIMENTAL_CXX0X__ LOCAL_CPPFLAGS := -std=gnu++11 -pthread -frtti -fexceptions 

I made sure 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 several cpp / hpp tests. Everything compiles without any C ++ 11 features and includes something like <thread> or <memory> does not cause any complaints, however when I create the std :: thread object I get the "Type" std :: thread 'not can be resolved. "This also happens with other functions I'm trying to use (std :: unique_ptr, std :: shared_ptr, std :: move () etc).

I read a lot of topics about C ++ 11 compilation, but nothing works, I think I missed something, but I can’t understand what it is. Any help would be appreciated!

EDIT: if I type __cplusplus it will display 201103L, so it looks like it is using the correct version.

EDIT 2: std :: atomic seems to work fine.

+6
source share
2 answers

I apologize, the following should have been a comment, not a reply - because I don’t know what is wrong in your code, but here is what you can do to figure out yourself:


Here is 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 a clean r9b string on my Mac. One thing to check: run ndk-build V=1 and make sure 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 the output of the 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() 
+3
source

You may need to invert the line:

 LOCAL_MODULE := RAGEAndroid 

with line:

 LOCAL_CPPFLAGS := -std=c++11 -pthread -frtti -fexceptions 

To say that these flags should be used in the current module (RageAndroid), and not in the previous module.

By the way, in my project I need to specify C ++ 11 with the line above (C ++ 0x ↔ C ++ 11) (otherwise it did not work here).

 LOCAL_CPPFLAGS := -std=c++0x 
0
source

Source: https://habr.com/ru/post/958211/


All Articles