Link problem when using address sanitizer with android NDK - android-ndk

I'm using Android Studio to build an app containing a module that uses the NDK. There is evidence of memory corruption so I'm trying the Address Sanitizer, following these instructions on the NDK developer site. But the app won't build.
I need to (A) ensure I'm targeting Android 27+ (I set minSdkVersion to 27; I'm building a debug build for a Galaxy S9, SDK 28), and (B) add compiler flags, which I've done:
android {
defaultConfig {
externalNativeBuild {
cmake {
# Can also use system or none as ANDROID_STL.
arguments "-DANDROID_ARM_MODE=arm", "-DANDROID_STL=c++_shared"
cppFlags "-fsanitize=address -fno-omit-frame-pointer"
}
}
}
}
I've also added wrap.sh scripts according to the instructions but I understand that they become relevant only at runtime.
The problem is that my app won't build. The output follows.
The C++ compiler
"C:/Users/user/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir:
C:/Users/user/studio/app/android/audioengine/.externalNativeBuild/cmake/debug/arm64-v8a/CMakeFiles/CMakeTmp
Run Build
Command:"C:\Users\user\AppData\Local\Android\sdk\cmake\3.6.4111459\bin\ninja.exe"
"cmTC_58655"
[1/2] Building CXX object
CMakeFiles/cmTC_58655.dir/testCXXCompiler.cxx.o
[2/2] Linking CXX executable cmTC_58655
FAILED: cmd.exe /C "cd . &&
C:\Users\user\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe
--target=aarch64-none-linux-android27 --gcc-toolchain=C:/Users/user/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64
--sysroot=C:/Users/user/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot
-g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -fsanitize=address -fno-omit-frame-pointer -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--gc-sections CMakeFiles/cmTC_58655.dir/testCXXCompiler.cxx.o -o cmTC_58655 -latomic -lm && cd ."
C:/Users/user/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin\ld:
warning: liblog.so, needed by
C:\Users\tim\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\lib64\clang\8.0.2\lib\linux\libclang_rt.asan-aarch64-android.so,
not found (try using -rpath or -rpath-link)
clang++.exe: error: linker command failed with exit code 1 (use -v
to see invocation)
The compiler flags have been passed correctly. There is a warning concerning liblog.so not being found, but then a non-specific error.
The instructions show where to place the sanitizer libraries in the project (in the jniLibs folder), but not where to source them. I copied them from the NDK install on my machine. I tried doing the same with liblog libraries but It's not clear which variant to use; the one I tried (for SDK 28) didn't affect the result.
What am I missing? I've found posts struggling with understanding exactly how to use the address sanitizer, but none mentions this particular problem.

Looks like those docs are wrong. It seems that CMake isn't using all of the linker flags it needs when performing that test. I'm not sure if that's NDK bug or a CMake bug, but here's a way to make ASan work with CMake/gradle:
Remove the cppFlags section from your build.gradle
Add those options in your CMakeLists.txt instead, like so:
add_library(app SHARED app.cpp)
target_compile_options(app PUBLIC -fsanitize=address -fno-omit-frame-pointer)
set_target_properties(app PROPERTIES LINK_FLAGS -fsanitize=address)
I've uploaded a change to fix the docs. Should be live soon.

Related

include openssl library on native android project

I want to create wrapper module to openssl library for android that will use most of the main functions for security.
I know there are people that say they already did it and publish thier work on github but for security reasons we can't use it.
I compiled the openssl library to android and add to the CMakeList.txt as described here:
https://developer.android.com/studio/projects/configure-cmake#add-other-library
My steps that i have made :
1. I download from here: https://www.openssl.org/source/ version 1.1.1c
2. Compiled sucessfully:
export ANDROID_NDK_HOME=/home/user/Android/Sdk/ndk-bundle
PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
./Configure android-arm64
make
I put the entire folder here:
Project/app/src/main/cpp/openssl-1.1.1c
4.thats how the CMakeList looks like:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE ON)
add_library(openssl-wrapper-lib
SHARED
openssl-c-wrapper.cpp)
#===v===v===v===v===v===v===openssl-include===v===v===v===v===v===v===
add_library(
crypto
SHARED
IMPORTED
)
set_target_properties(
crypto
PROPERTIES IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/openssl-1.1.1c/libcrypto.so
)
include_directories(openssl-1.1.1c/include)
#===^===^===^===^===^===^===openssl-include===^===^===^===^===^===^===
find_library(log-lib
log)
target_link_libraries( # Specifies the target library.
openssl-wrapper-lib
crypto
${log-lib})
the error when i build is this:
Error while executing process /home/user/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/user/AndroidStudioProjects/EncryptionModule/app/.externalNativeBuild/cmake/debug/x86_64 --target openssl-wrapper-lib}
[1/1] Linking CXX shared library /home/user/AndroidStudioProjects/EncryptionModule/app/build/intermediates/cmake/debug/obj/x86_64/libopenssl-wrapper-lib.so
FAILED: : && /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=x86_64-none-linux-android21 --gcc-toolchain=/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -v -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libopenssl-wrapper-lib.so -o /home/user/AndroidStudioProjects/EncryptionModule/app/build/intermediates/cmake/debug/obj/x86_64/libopenssl-wrapper-lib.so CMakeFiles/openssl-wrapper-lib.dir/openssl-c-wrapper.cpp.o /home/user/AndroidStudioProjects/EncryptionModule/app/src/main/cpp/openssl-1.1.1c/libcrypto.so /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/liblog.so -latomic -lm && :
Android (5220042 based on r346389c) clang version 8.0.7 (https://android.googlesource.com/toolchain/clang b55f2d4ebfd35bf643d27dbca1bb228957008617) (https://android.googlesource.com/toolchain/llvm 3c393fe7a7e13b0fba4ac75a01aa683d7a5b11cd) (based on LLVM 8.0.7svn)
Target: x86_64-none-linux-android21
Thread model: posix
InstalledDir: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/i686-linux-android/4.9.x
Found candidate GCC installation: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x
Selected GCC installation: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x
Candidate multilib: .;#m64
Selected multilib: .;#m64
"/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld" --sysroot=/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -z now -z relro --hash-style=gnu --hash-style=both --enable-new-dtags --eh-frame-hdr -m elf_x86_64 -shared -o /home/user/AndroidStudioProjects/EncryptionModule/app/build/intermediates/cmake/debug/obj/x86_64/libopenssl-wrapper-lib.so /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/crtbegin_so.o -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.7/lib/linux/x86_64 -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/lib/../lib64 -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21 -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/lib -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib --exclude-libs libgcc.a --exclude-libs libatomic.a --build-id --warn-shared-textrel --fatal-warnings --no-undefined -z noexecstack -soname libopenssl-wrapper-lib.so CMakeFiles/openssl-wrapper-lib.dir/openssl-c-wrapper.cpp.o /home/user/AndroidStudioProjects/EncryptionModule/app/src/main/cpp/openssl-1.1.1c/libcrypto.so /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/liblog.so -latomic -lm -Bstatic -lc++ -Bdynamic -lm -lgcc -ldl -lc -lgcc -ldl /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/crtend_so.o
/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/user/AndroidStudioProjects/EncryptionModule/app/src/main/cpp/openssl-1.1.1c/libcrypto.so: incompatible target
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
please help me understand what am I doing wrong
You built the library for arm64, but you're also building for Android's other ABIs. You need to build those libraries for each architecture your application supports. i.e., repeat your initial steps:
./Configure android-arm
make
./Configure android-arm64
make
./Configure android-x86
make
./Configure android-x86_64
make
(Note that you probably need to run each of those builds in a separate directory, unless the build scripts you're using will install libraries for each arch independently for you.)
You'll then want to do something like the following (adjust as needed based on your actual install paths) to import the libraries into CMake:
set_target_properties(
crypto
PROPERTIES IMPORTED_LOCATION
# The change happens here: each of the architectures gets its own subdirectory.
${CMAKE_CURRENT_SOURCE_DIR}/openssl-1.1.1c/${ANDROID_ABI}/libcrypto.so
)
The other option, if you only care about supporting 64-bit ARM, would be to disable the other architectures in your build.gradle, as described by https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.NdkOptions.html:
android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
ndk {
// Tells Gradle to build outputs for the following ABIs and package
// them into your APK.
abiFilters 'arm64-v8a'
}
}
}

Compiling program with Open Source libFTDI

I use Asus router (based on ARMv7 proc) with Advanced Tomato installed
on it as my ARMv7 developer platform. I install compiler (gcc - 5.4.0-1)
plus dependencies and libFTDI (libftdi1 - 1.3-1) from OpenWRT Linux
repo. OpenWRT does not provide libftdi-dev so I copied ftdi.h file from libFTDI download page to /opt/include directly. I try to compile program taken directly from libFTDI samples. The
compiler command is:
gcc -v -Wl,-rpath=/opt/usr/local/lib -Wl,--dynamic-linker=/opt/lib/ld-linux.so.3 -L/opt/lib -O2 -pipe -march=armv7-a -mtune=cortex-a9-fno-caller-saves -mfloat-abi=soft -l ftdi1 d.c -o d
But compilation fails because:
/opt/bin/ld: cannot find -lftdi1
But there is /opt/usr/local/lib/libftdi1.so linked to libftdi1.so.2
My LD_LIBRARY_PATH looks like this:
/lib:/usr/lib:/usr/local/lib:/opt/lib:/opt/usr/lib:/opt/include:/opt/usr/local/lib:/opt/usr/include
So what the problem is?
I dont know why (probably bug) but for compiler taken from OpenWRT repo, MUST have wanted library in /opt/lib. So simply copy libftdi1.so.2.3.0 file and linking it to libftdi1.so resolved problem. That means that it does not use correctly LD_LIBRARY_PATH variable. Finally compilation command looks like this:
gcc -v -Wl,-rpath=/opt/usr/local/lib -Wl,--dynamic-linker=/opt/lib/ld-linux.so.3 -L/opt/lib -O2 -pipe -march=armv7-a -mtune=cortex-a9 -fno-caller-saves -mfloat-abi=soft -l ftdi1 arco.c -o arco
From my point of view - topic closed

Source-built clang-3.7 on Linux not finding Block_copy

I built and installed llvm/clang-3.7 from source on my Ubuntu Linux system (I'm building from source because my development environment at work does not have apt-get available). The gcc version is 4.8.2. I followed the clang build instructions at http://clang.llvm.org/get_started.html, and everything worked fine (mkdir build; cd build; cmake -G "Unix Makefiles" ../llvm; make; make install). However, I'm now finding that a program to test Block_copy fails to compile. The program is auto-generated by autoconf when I try to build gnustep-base. The part that fails is:
int
main ()
{
return _Block_copy ();
;
return 0;
}
My compile command is:
clang -o conftest -m64 -march=opteron -mno-3dnow -ggdb -O2 -Wall -I/home/build/GNUstep/Local/Library/Headers -I/home/build/GNUstep/Local/Library/Headers -I/home/build/GNUstep/System/Library/Headers -fgnu-runtime -x objective-c -m64 -L/home/build/GNUstep/Local/Library/Libraries -L/home/build/GNUstep/Local/Library/Libraries -L/home/build/GNUstep/System/Library/Libraries conftest.c -lrt -ldl -lpthread -rdynamic -m64 -fgnu-runtime -L/home/build/GNUstep/Library/Libraries -L/home/build/GNUstep/Local/Library/Libraries -L/home/build/GNUstep/System/Library/Libraries -lobjc -lm
Do I need to build clang with a special option to enable blocks, or should I be linking with another library?
Do I need to build clang with a special option to enable blocks
No. But you may need -fblocks option using the clang binary.
-fblocks
Enable the "Blocks" language feature.
should I be linking with another library?
AFAIK, Yes.
_Block_copy is a part of BlocksRuntime.
BlocksRuntime Block.h
BlocksRuntime runtime.c
Have you compiled compiler-rt? It includes BlocksRuntime. The document explains how to build compiler-rt.

undefined reference when cross-compiling for ARM with static OpenCV libraries

I'm trying to compile a simple test program using static OpenCV libraries that have been compiled using an ARM compiler. But when I try to compile it with the command
$arm-linux-gnueabihf-g++ `pkg-config --static opencv` -I/usr/local/include -L<path to static libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2
This gives
/tmp/ccxNeUbK.o: In function main':
ARMtest2.cpp:(.text+0x8a): undefined reference tocv::Mat::ones(int, int, int)'
/tmp/ccxNeUbK.o: In function cv::Mat::~Mat()':
ARMtest2.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x20): undefined reference tocv::fastFree(void*)'
/tmp/ccxNeUbK.o: In function cv::Mat::release()':
ARMtest2.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x30): undefined reference tocv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
The code itself is just some simple test code that prints a Mat type variable.
I compiled the static OpenCV library with cmake-gui. I selected UNIX Makefile and then selected 'specify options for cross-compiling' where I gave the path to the ARM (arm-linux-gnueabihf) gcc and g++ compiler. Then I unticked BUILD_SHARED_LIB so it compiled static libraries. It seemed to compile fine without errors. After that I did make & sudo make install.
I also tried it with shared libraries and that worked fine on the ARM board (once I copied the libraries to the board and exported the library path).
The static .a files landed nicely in the build folder. Apparently it can also find it when I -L link to it. I have tried reversing the order of the libraries, but to no avail.
So I'm a bit at a loss what is going wrong.
I solved it. Using the normal --static pkg-config command to compile with OpenCV libraries;
`pkg-config --libs --static opencv`
Of course I installed the static libraries also to the folder /usr/local/lib where libraries get installed first. But still i don't know what I missed in the command line I tried to use. I had a look in the config file /usr/local/lib/pkgconfig/opencv.pc
Here is whats in it:
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.4.9
Libs: ${exec_prefix}/lib/libopencv_calib3d.so ${exec_prefix}/lib/libopencv_contrib.so ${exec_prefix}/lib/libopencv_core.so ${exec_prefix}/lib/libopencv_features2d.so ${exec_prefix}/lib/libopencv_flann.so ${exec_prefix}/lib/libopencv_gpu.so ${exec_prefix}/lib/libopencv_highgui.so ${exec_prefix}/lib/libopencv_imgproc.so ${exec_prefix}/lib/libopencv_legacy.so ${exec_prefix}/lib/libopencv_ml.so ${exec_prefix}/lib/libopencv_nonfree.so ${exec_prefix}/lib/libopencv_objdetect.so ${exec_prefix}/lib/libopencv_ocl.so ${exec_prefix}/lib/libopencv_photo.so ${exec_prefix}/lib/libopencv_stitching.so ${exec_prefix}/lib/libopencv_superres.so ${exec_prefix}/lib/libopencv_ts.a ${exec_prefix}/lib/libopencv_video.so ${exec_prefix}/lib/libopencv_videostab.so -lrt -lpthread -lm -ldl
Cflags: -I${includedir_old} -I${includedir_new}
I believe this is what is being called with the pkg-config <--something_or_other> opencv line.
And saw some other things that probably get linked when compiling -lrt -lpthread -lm -ldl Not really sure though as I tried it ina normal command line and I apparently still missed somethings.
But it worked, so didn't really bother too much with it much further :)
In your command:
*$arm-linux-gnueabihf-g++ `pkg-config --static opencv` -I/usr/local/include -L<path to static libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2*
The cflags was missing.
Try with:
$arm-linux-gnueabihf-g++ `pkg-config --cflags --static opencv` -I/usr/local/include -L<path to static cross-compiled libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2
Assuming here that your cross-compiled headers are saved in /usr/local/include (as you specified).

Android NDK r5b external build and supc++ link problem

I am attempting to cross-compile our C++ code base (using CMake) for the Android platform using the r5b NDK on Ubuntu 10.10. The compile phase succeeds, however during the final link phase for the .so there are many unresolved references to symbols that are in the libsupc++.a file (which I specify to link in). I have also tried -lsupc++ with no difference.
I tried to follow the command-line as closely as possible as generated by the official ndk-build system when building the test-gnustl-1 NDK test app.
Running the arm-linux-androideabi-nm tool on the arm-linux-androideabi/lib/libsupc++.a file shows the symbols as defined (T) referenced in the error output.
An example of a symbol defined in libsupc++ it cannot find is: __gxx_personality_v0
Here is my sample link line and a resulting sample of errors.
/home/user/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-g++ -fPIC -Wall -Wextra -Wno-unused -Wno-multichar -fno-rtti -MMD -MP -MF -ffunction-sections -fexceptions -funwind-tables -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -Wa,--noexecstack -DANDROID -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -O0 -DDEBUG -D_DEBUG -g --sysroot=/home/user/android-ndk-r5b/platforms/android-9/arch-arm -Wl,--gc-sections -Wl,-z,nocopyreloc -Wl,--no-undefined,-z,noexecstack -L/home/user/android-ndk-r5b/platforms/android-9/arch-arm/usr/lib -L/home/user/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/lib -Wl,-rpath-link=/home/user/android-ndk-r5b/platforms/android-9/arch-arm/usr/lib /home/user/android-ndk-r5b/sources/cxx-stl/gnu-libstdc++/libs/armeabi/libstdc++.a -lc -lsupc++ -shared -o ../../lib/Debug/libFoo.so CMakeFiles/Foo.dir/Foo.c.o CMakeFiles/Foo.dir/Bar.cpp.o CMakeFiles/Foo.dir/Baz.cpp.o
`CMakeFiles/Foo.dir/Foo.cpp.o: In function 'myFunc':
/home/user/myroj/src/native/modules/libFoo/Foo.cpp:292: undefined reference to '__cxa_end_cleanup'
CMakeFiles/Foo.dir/Foo.cpp.o:(.ARM.extab.text.myFunc+0x0): undefined reference to '__gxx_personality_v0'
...
/home/user/android-ndk-r5b/toolchains/android/sources/cxx-stl/gnu-libstdc++/include/bits/vector.tcc:350: undefined reference to '__cxa_begin_catch'
/home/user/android-ndk-r5b/toolchains/android/sources/cxx-stl/gnu-libstdc++/include/bits/vector.tcc:357: undefined reference to '__cxa_rethrow'
/home/user/android-ndk-r5b/toolchains/android/sources/cxx-stl/gnu-libstdc++/include/bits/stl_vector.h:1153: undefined reference to 'std::__throw_length_error(char const*)'
/home/user/android-ndk-r5b/toolchains/android/sources/cxx-stl/gnu-libstdc++/include/bits/stl_tree.h:199: undefined reference to 'std::_Rb_tree_decrement(std::_Rb_tree_node_base*)'
...
I tried to creating a simple hello-world app that makes use of exceptions and links in a simple .a file. I got the same errors until I linked in the libsupc++.a library directly instead of using '-lsupc++'. However, this same technique did not work on the larger project link step. The NDK docs also suggest '-lsupc++' is should be used when using external build tools.
I am out of ideas on how to resolve this linking issue. I have tried reordering the link line as many ways as I could think of. I know linking in general can be a fickle process.
Any help is greatly appreciated.
It turns out I needed to add -L/home/user/android-ndk-r5b/sources/cxx-stl/gnu-libstdc++/libs/armeabi" to the path and explicitly link "/home/user/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/lib/thumb/libsupc++.a" with all include libraries wrapped in a "--start-group --end-group" clause.
Hopefully this helps someone else too.

Resources