Android NDK clang compiler can't find std::make_unique - android

I'm using Android NDK r10d. My application.mk is setup like so:
APP_CFLAGS := -DANDROID -DBUILD_OGLES2
APP_CPPFLAGS := $(APP_CFLAGS) -fexceptions -frtti -std=c++14
APP_STL := gnustl_static
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15
NDK_TOOLCHAIN_VERSION := clang
I am using std::make_unique in my code and it isn't compiling (says it isn't found). This feature should be available in STL starting with C++14. I did some poking around and it seems that clang isn't using GNU STL 4.9 in the NDK. If it were, it would be available since I see it inside <memory> header.
What am I missing here? Is there a way to use 4.9 GNU STL with clang?

make_unique isn't available through gnustl from clang. You can try using LLVM libc++ instead. Set this inside your Application.mk:
APP_STL := c++_static
NDK_TOOLCHAIN_VERSION := clang
edit:
Forcing the use of GNU STL 4.9 (by changing TOOLCHAIN_VERSION inside android-ndk-r10d/toolchains/*toolchain_name*-clang3.5/setup.mk) makes the build crash:
clang++: /s/ndk-toolchain/src/llvm-3.5/llvm/tools/clang/lib/AST/DeclBase.cpp:1293: clang::DeclContext::lookup_result clang::DeclContext::lookup(clang::DeclarationName): Assertion 'DeclKind != Decl::LinkageSpec && "Should not perform lookups into linkage specs!"' failed.

Related

C++11 random library in Android JNI

I am trying to compile an Android app with a native component that uses the C++ random library.
My Application.mk file is:
APP_STL := stlport_static
APP_CPPFLAGS += -std=gnu++11
NDK_TOOLCHAIN_VERSION := 4.8
On compilation I get the error:
[armeabi] Compile++ thumb: Project <= main.cpp
/home/user/project/main.cpp:12:18: fatal error: random: No such file or directory
#include <random>
Is the random library available for Android?
APP_STL := stlport_static
APP_CPPFLAGS += -std=gnu++11
NDK_TOOLCHAIN_VERSION := 4.8
You're building agains STLPort - which as far as I can tell - is a C++03 standard library implementation. Hence it won't have C++11 headers.
Of the other options, you might consider:
APP_STL:= gnustl_static
or
APP_STL:= c++_static
Which give you GNU libstd++ or LLVM's libc++ respectively. If you are concerned about GPL polution to your app (as Google clearly are not using libstd++ by default), use libc++. You might as well you clang as the compiler as well at this point.

NDK project with clang: linker errors

I have a perfectly working NDK project that uses the latest NDK r10d and GCC 4.9 toolchain. I've decided to try building it with clang instead. I've set NDK_TOOLCHAIN_VERSION := clang in Application.mk, and also replaced APP_STL := gnustl_static with APP_STL := c++_static. I didn't make any other adjustments, because upon Googling the matter for a while, I decided I don't need any. I have successfully compiled all the sources, but linking throws a set of 3 errors for every single object file (error message in bold):
c:/Development/Android_SDK/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin\ld.exe:
error: ./obj/local/armeabi-v7a/objs/native_lib/main.o:1:3:
invalid character
c:/Development/Android_SDK/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin\ld.exe:
error: ./obj/local/armeabi-v7a/objs/native_lib/main.o:1:3:
syntax error, unexpected $end
c:/Development/Android_SDK/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin\ld.exe:
error: ./obj/local/armeabi-v7a/objs/native_lib/main.o: not
an object or archive
This happens in release (APP_OPTIM := release APP_CFLAGS := -O3), but not in debug (APP_OPTIM := debug APP_CFLAGS := -O0 -g -gdwarf-2). With debug build, everything's fine!
Here's my entire Application.mk:
APP_OPTIM := release
APP_CFLAGS := -O3 -flto -fwhole-program
APP_STL := c++_static
APP_CPPFLAGS += -fexceptions -frtti -std=c++11 -rdynamic -funwind-tables
APP_CFLAGS += -Wno-deprecated-declarations -rdynamic -funwind-tables -Wno-deprecated-register
GLOBAL_ARM_MODE := arm
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15
NDK_TOOLCHAIN_VERSION := clang
APP_GNUSTL_FORCE_CPP_FEATURES := exceptions rtti
#Position-independent executable
APP_PIE := true
APP_MODULES := native_lib
What could be the problem here?
Note that it uses ld from GCC 4.8, is that correct?
P. S. This is on Windows x64.

Using thread and mutex in Android Studio with ndk 10c

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.
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

build android with clang instead of gcc ? and the clang stl lib instead of gnustl lib?

Am trying to build an android ndk app using clang instead of gcc, for know i have tried this
in the Android.mk
NDK_TOOLCHAIN_VERSION := clang
LOCAL_CLANG :=true
LOCAL_LDLIBS := -lc++_static
LOCAL_CFLAGS := -std=c++11
and in the Application.mk
APP_PLATFORM := android-9
APP_STL := libc++_static
APP_CPPFLAGS := -fexceptions -frtti
APP_ABI := armeabi-v7a
but it always give me link errors with the std library.
Any help is appreciated !
There are several mistakes in your *.mk files:
libc++_static isn't a proper value for APP_STL, it should be c++_static here.
NDK_TOOLCHAIN_VERSION has no effect when set inside Android.mk, it should be set inside Application.mk
LOCAL_CLANG is a variable used inside system modules from AOSP, not when using the NDK.
Since you're setting APP_STL as c++_static, the NDK toolchain will correctly tell the linker what lib to use, you shouldn't add LOCAL_LDLIBS := -lc++_static.
Also, you set APP_ABI to only armeabi-v7a, is it on purpose ? Android runs on other architectures as well and you'll get better performance on these if you also compile your libraries accordingly. You can either set APP_ABI to all or to a list of architectures armeabi-v7a x86...
In summary:
Android.mk
LOCAL_CFLAGS := -std=c++11
Application.mk
NDK_TOOLCHAIN_VERSION := clang
APP_PLATFORM := android-9
APP_STL := c++_static
APP_CPPFLAGS := -fexceptions -frtti
APP_ABI := all
If you continue having some troubles compiling your code, please show the exact errors you're getting.
The building settings are correct,
mostly this is happens because you are linking with library that use gcc instead of clang. check if all your linked library using clang !

Getting android NDK r9d to have C++11 support

I can't seem to get the android NDK to have C++11 support. Considering I'm only porting another person's project to android through the ndk, I really don't have any experience with c++ before now.
My application.mk:
NDK_TOOLCHAIN_VERSION := clang
APP_STL := c++_static
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions
APP_ABI := all
APP_PLATFORM := android-8
Thanks in advance!

Categories

Resources