Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK r9b and compiling C++11

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.

like image 497
Rajveer Avatar asked Nov 16 '13 17:11

Rajveer


People also ask

What compiler does android NDK use?

Code written in C/C++ can be compiled to ARM, or x86 native code (or their 64-bit variants) using the Android Native Development Kit (NDK). The NDK uses the Clang compiler to compile C/C++.

What is android NDK used for?

The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

What is LIBC android?

LLVM's libc++ is the C++ standard library that has been used by the Android OS since Lollipop, and as of NDK r18 is the only STL available in the NDK. Note: For full details of the expected level of C++ library support for any given version, see the C++14 Status, C++17 Status, and C++20 Status pages.

Where do I put android NDK?

Select the NDK (Side by side) checkbox and the checkboxes below it that correspond to the NDK versions you want to install. Android Studio installs all versions of the NDK in the android-sdk /ndk/ directory.


1 Answers

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()
like image 107
Alex Cohn Avatar answered Oct 28 '22 11:10

Alex Cohn