6

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.

Rajveer
  • 847
  • 11
  • 28

2 Answers2

3

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()
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Hi, thanks for the detailed reply! The link stage looked the same, and I'm running on Windows so don't have grep but by opening libgnustl_static.a in Notepad++ I was able to find instances of other std:: objects, but nothing related to std::thread. Which leads me to believe that the library was compiled with support off? I built another minimal project based on your example which also failed to compile with threads. Here is a link to it, if you don't mind could you see if it builds in your setup? https://www.dropbox.com/s/16bcbuokc1echo7/JNITest.rar – Rajveer Nov 17 '13 at 19:09
  • I downloaded the SDK and NDK on OSX and tried compiling, and end up with the same issues so it's definitely something I'm doing. In OSX the grep command gave the same output as you have. Finally I've put in the ndk-build V=1 result in this file just in case (this is from Windows): http://www.dropbox.com/s/who4cqkogoqph9b/ndk-build%20verbose.txt – Rajveer Nov 17 '13 at 20:34
  • I don't see problems in the `ndk-build V=1` [result](https://www.dropbox.com/s/who4cqkogoqph9b/ndk-build%20verbose.txt). Maybe, I misunderstand your complaint? – Alex Cohn Nov 18 '13 at 07:23
0

May be you need to inverse the line:

LOCAL_MODULE    := RAGEAndroid

with the line:

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

To say these flags need to be use on the current module (RageAndroid), not the previous module.

By the way, in my project I need to indicate c++11 with the line above (c++0x<->c++11) (otherwise it didn't work here).

LOCAL_CPPFLAGS  := -std=c++0x
Dono
  • 128
  • 1
  • 9
  • Tried both of those things and they didn't work. In the end I couldn't figure out what was wrong, so I took the ThreadingProblem project that Jak made from [here](https://code.google.com/p/android/issues/detail?id=58616) and just stripped everything out. I did a diff but still can't see what's different :/ – Rajveer Nov 21 '13 at 16:49