Getting android NDK r9d to have C++11 support - android

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!

Related

Android NDK build to support all available devices

I am using in my project both java files and native c++ code. I would like to make the app available for all devices and hardware (API lvl >= 15). I am also using OpenCV4Android both in java and my native c++ if that matters. I'm not quite sure if my current configuration is sufficient to support all available hardware:
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-9
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include C:/OpenCV4Android/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := myNativeLib
LOCAL_SRC_FILES := myNativeLib.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
I have tested this configuration on a few devices and it seems to work but doesn't APP_ABI := armeabi-v7a narrow down the supported devices to only those with armeabi-v7a, or actually every device will be able to run it? Maybe I should insert APP_ABI := all as mentioned here? https://developer.android.com/ndk/guides/application_mk.html
It is very important to me to make the app responding and working for all devices possible.
To support all platforms you should use APP_ABI := all
. For API 15 use APP_PLATFORM := android-15.
Pay attention that the more platforms you will support the larger your apk file will be, since it will create a binary for each supported platform in your APK.

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.

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

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.

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 !

Compile NDK code using gnu libstdc++

I want to compile my NDK code using gnu libstdc++, any clue how to do this?
You should add a line to Application.mk
APP_STL := gnustl_static
if you want to link it statically, and
APP_STL := gnustl_shared
if you want to use it as a shared library.
Here is the example of typical Application.mk (it should be placed into the same folder where your Android.mk is located):
APP_OPTIM := release
APP_PLATFORM := android-7
APP_STL := gnustl_static
APP_CPPFLAGS += -frtti
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -DANDROID
APP_ABI := armeabi-v7a
More information on Application.mk can be found in your NDK docs: docs/APPLICATION-MK.html
Add the following line to your Application.mk:
APP_STL := gnustl_static
(or gnustl_shared if you don't want to link statically against it).

Categories

Resources