Compile NDK code using gnu libstdc++ - android

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

Related

How to generate arm64-v8a 64bit shared object from c++ code?

I've been using a c++ library on my android app for some time now but it has been using 32-bit. I'm trying to compile now the c++ library to a 64bit .so file but every time I do ndk-build I do see the armeabi-v7a get generated successfully, but when the arm64-v8a tries to compile, it fails and gives me the following error message:
libfftw3.a: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
I'm going to place below my Application.mk and Android.mk respectively to show what I've done to try to do this properly.
Application.mk
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION=4.9
APP_CPPFLAGS += -frtti
APP_PLATFORM := android-23
APP_CPPFLAGS += -std=c++11
APP_ABI := armeabi-v7a arm64-v8a
APP_MODULES := SCCpp
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
CXXFLAGS += -arch x86_64 -arch i386
LOCAL_MODULE := fftw3
LOCAL_SRC_FILES := ../lib/libfftw3.a
LOCAL_EXPORT_C_INCLUDES = $(LOCAL_PATH)/..
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
GLOBAL_C_INCLUDES := \
$(LOCAL_PATH)/../src
LOCAL_SRC_FILES += ../src/d/re.cpp
LOCAL_SRC_FILES += ../src/d/me.cpp
LOCAL_SRC_FILES += ../src/d/ev.cpp
LOCAL_SRC_FILES += ../src/d/fe.cpp
LOCAL_SRC_FILES += ../src/fft/fourier.cpp
LOCAL_SRC_FILES += ../src/nu/ac.cpp
LOCAL_SRC_FILES += ../src/nu/st.cpp
LOCAL_SRC_FILES += ../src/utils/converter.cpp
LOCAL_SRC_FILES += ../src/sci.cpp
LOCAL_MODULE := SCCpp
LOCAL_MODULE_FILENAME := libSCCpp
LOCAL_STATIC_LIBRARIES := fftw3
LOCAL_CPP_FEATURES += exceptions
LOCAL_CFLAGS += -fopenmp
LOCAL_CFLAGS += -Ofast
LOCAL_LDFLAGS += -fopenmp -t
LOCAL_CPPFLAGS := -std=c++11
LOCAL_LDLIBS += -latomic -llog
LOCAL_C_INCLUDES := $(GLOBAL_C_INCLUDES) $(LOCAL_PATH)/../../../3rdParty/fftw-3.3.4-dll32/
LOCAL_EXPORT_CPPFLAGS := -fexceptions -frtti
include $(BUILD_SHARED_LIBRARY)
I'm really unfamiliar with this whole process so I'm at a dead end especially since the logs don't really tell me anything at all. I thought that by just adding in the arm64-v8a would automatically generate the 64bit shared object. Is there a missing step?
If there's any missing information that I should provide, please ask in the comments so I can give more details, but as of now in my current position, this is all that I have touched in the code base for the c++ library so I'm not quite sure where all the pieces are at the moment.
You use a prebuilt library at
../lib/libfftw3.a
You must build fftw3 for arm64, too. Usually we have
LOCAL_SRC_FILES := ../lib/$(TARGET_ARCH_ABI)/libfftw3.a
so that ndk-build can choose the right variant of the 3rd party lib.
The instructions to build fftw3 for arm64 are available on GitHub.

Ndk error Application target deprecated

Error:(81) Android NDK: Application targets deprecated ABI(s): mips64 armeabi mips
Error:(82) Android NDK: Support for these ABIs will be removed in a future NDK release.
This is my Application.mk file
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti
APP_CPPFLAGS := -fexceptions -frtti
APP_CPPFLAGS +=-std=c++11
APP_CPPFLAGS +=-fpermissive
APP_ABI=armeabi-v7a
Redirecting to setup-app.mk
_deprecated_abis := $(filter $(NDK_DEPRECATED_ABIS),$(NDK_APP_ABI))
ifneq ($(_deprecated_abis),)
$(call __ndk_warning,Application targets deprecated ABI(s): $(_deprecated_abis))
$(call __ndk_warning,Support for these ABIs will be removed in a future NDK release.)
endif
I downloaded the ndk through Android studio.
If you run ndk-build via gradle, it ignores the APP_ABI in your Application.mk. In this case, your build.gradle should define abiFilters, e.g.
android {
ndk {
abiFilters "armeabi-v7a"
}
}

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.

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