I want to use clang++-3.5 and libc++ from CrystaX (10.1.0) to compile a native component which uses boost + ICU (using boost's u32regex_match etc.). It compiles and links fine using clang++-3.5 and libc++ on my desk machine. It compiles fine using CrystaX, but I get a bunch of "undefined reference" link errors:
[powercf@powercf-desktop]computing/programming/crystax_test% rm -rf app/src/main/{obj,libs}; /home/powercf/Android/Ndk/ndk-build V=1 -C /home/powercf/computing/programming/crystax_test/app/src/main
make: Entering directory `/home/powercf/computing/programming/crystax_test/app/src/main'
rm -f ./libs/arm64-v8a/lib*.so ./libs/armeabi/lib*.so ./libs/armeabi-v7a/lib*.so ./libs/armeabi-v7a-hard/lib*.so ./libs/mips/lib*.so ./libs/x86/lib*.so ./libs/x86_64/lib*.so
rm -f ./libs/arm64-v8a/gdbserver ./libs/armeabi/gdbserver ./libs/armeabi-v7a/gdbserver ./libs/armeabi-v7a-hard/gdbserver ./libs/mips/gdbserver ./libs/x86/gdbserver ./libs/x86_64/gdbserver
rm -f ./libs/arm64-v8a/gdb.setup ./libs/armeabi/gdb.setup ./libs/armeabi-v7a/gdb.setup ./libs/armeabi-v7a-hard/gdb.setup ./libs/mips/gdb.setup ./libs/x86/gdb.setup ./libs/x86_64/gdb.setup
[armeabi] Compile++ thumb: test-boost <= test.cpp
/home/powercf/Android/crystax-ndk-10.1.0/toolchains/llvm-3.5/prebuilt/linux-x86_64/bin/clang++ -MMD -MP -MF ./obj/local/armeabi/objs/test-boost/test.o.d -gcc-toolchain /home/powercf/Android/crystax-ndk-10.1.0/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64 -fpic -ffunction-sections -funwind-tables -fstack-protector -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes -fno-integrated-as -target armv5te-none-linux-androideabi -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -I/home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/include -I/home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/include -I/home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/include -I/home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/include -I/home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/include -I/home/powercf/Android/crystax-ndk-10.1.0/sources/cxx-stl/llvm-libc++/libcxx/include -I/home/powercf/Android/crystax-ndk-10.1.0/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/libcxxabi/include -Ijni -DANDROID -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -frtti -fexceptions -c -I/home/powercf/Android/crystax-ndk-10.1.0/sources/crystax/include -I/home/powercf/Android/crystax-ndk-10.1.0/platforms/android-3/arch-arm/usr/include jni/test.cpp -o ./obj/local/armeabi/objs/test-boost/test.o
[armeabi] SharedLibrary : libtest-boost.so
/home/powercf/Android/crystax-ndk-10.1.0/toolchains/llvm-3.5/prebuilt/linux-x86_64/bin/clang++ -Wl,-soname,libtest-boost.so -shared --sysroot=/home/powercf/Android/crystax-ndk-10.1.0/platforms/android-3/arch-arm ./obj/local/armeabi/objs/test-boost/test.o /home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/libs/armeabi/libboost_regex.a /home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/libs/armeabi/libicui18n.a /home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/libs/armeabi/libicuio.a /home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/libs/armeabi/libicuuc.a /home/powercf/Android/crystax-ndk-10.1.0/sources/icu/54.1/libs/armeabi/libicudata.a /home/powercf/Android/crystax-ndk-10.1.0/sources/cxx-stl/llvm-libc++/libs/armeabi/thumb/libc++_static.a -lgcc -gcc-toolchain /home/powercf/Android/crystax-ndk-10.1.0/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64 -no-canonical-prefixes -target armv5te-none-linux-androideabi -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -mthumb -L/home/powercf/Android/crystax-ndk-10.1.0/sources/crystax/empty /home/powercf/Android/crystax-ndk-10.1.0/sources/crystax/libs/armeabi/thumb/libcrystax.so -lc -o ./obj/local/armeabi/libtest-boost.so
/home/powercf/Android/crystax-ndk-10.1.0/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: warning: hidden symbol '__cxa_call_unexpected' in /home/powercf/Android/crystax-ndk-10.1.0/sources/cxx-stl/llvm-libc++/libs/armeabi/thumb/libc++_static.a(cxa_personality.o) is referenced by DSO /home/powercf/Android/crystax-ndk-10.1.0/sources/crystax/libs/armeabi/thumb/libcrystax.so
/home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/libs/armeabi/libboost_regex.a(cpp_regex_traits.o):cpp_regex_traits.cpp:function boost::re_detail::cpp_regex_traits_char_layer<char>::init(): error: undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&)'
/home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/libs/armeabi/libboost_regex.a(cpp_regex_traits.o):cpp_regex_traits.cpp:function boost::re_detail::cpp_regex_traits_char_layer<char>::init(): error: undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
/home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/libs/armeabi/libboost_regex.a(cpp_regex_traits.o):cpp_regex_traits.cpp:function boost::re_detail::cpp_regex_traits_char_layer<char>::init(): error: undefined reference to 'std::string::_M_leak_hard()'
/home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/libs/armeabi/libboost_regex.a(cpp_regex_traits.o):cpp_regex_traits.cpp:function boost::re_detail::cpp_regex_traits_char_layer<char>::init(): error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator<char> const&)'
/home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/libs/armeabi/libboost_regex.a(cpp_regex_traits.o):cpp_regex_traits.cpp:function boost::re_detail::cpp_regex_traits_char_layer<char>::init(): error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator<char> const&)'
/home/powercf/Android/crystax-ndk-10.1.0/sources/boost/1.57.0/libs/armeabi/libboost_regex.a(cpp_regex_traits.o):cpp_regex_traits.cpp:function boost::re_detail::cpp_regex_traits_char_layer<char>::init(): error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator<char> const&)'
etc. etc.
I'm not an android, NDK, boost, icu, crystaX or clang expert, but I'm guessing the CrystaX boost & icu libraries aren't really compatible with clang? Or else I'm missing some config option or something? Is there any way to get this working (g++ + libstdc++ is also not working for me with the same code or with std::future so I'm not using that)? Or do I need to look at compiling my own boost and icu for android (which I'd rather avoid)?
test.cpp:
#include <jni.h>
#include <iostream>
#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
extern "C" jlong Java_com_powercf_examples_crystax_test_MainActivity_Foobie(JNIEnv* env, jobject thiz, jstring s)
{
boost::regex regex1("hello");
std::string input_str1("test");
bool match = boost::regex_match(input_str1, regex1);
UnicodeString regex_str = "tea";
UnicodeString input_str2 = "coffee";
boost::u32regex regex2 = boost::make_u32regex(regex_str);
match = boost::u32regex_match(input_str2, regex2);
return match;
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test-boost
LOCAL_SRC_FILES := test.cpp
LOCAL_STATIC_LIBRARIES := boost_regex_static icui18n_static icuio_static icuuc_static icudata_static
LOCAL_CPP_FEATURES := rtti exceptions
include $(BUILD_SHARED_LIBRARY)
$(call import-module,boost/1.57.0)
$(call import-module,icu/54.1)
Application.mk:
NDK_TOOLCHAIN_VERSION := clang3.5
APP_STL := c++_static
This happens because LLVM libc++ is not binary compatible with GNU libstdc++, and Boost in CrystaX NDK was built and linked against GNU libstdc++. The only way to fix it is build Boost with LLVM libc++, which shouldn't be too hard, but it's completely non-tested. I've filed ticket to do that in CrystaX NDK, but practically right now the fastest way would be to use GNU libstdc++ as C++ Standard Library implementation (it works with clang).
In other words, your Application.mk should be as below: