5

I have an issue when compiling my code using Boost for Android with Android Studio ndk. I am using one of the libraries libboost_filesystem-clang-mt-a32-1_66.a from boost, however, it shows following error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process /home/user/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/user/git/project/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
  [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
  [2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
  FAILED: : && /home/user/Downloads/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++  --sysroot=/home/user/Downloads/android-ndk-r16b/sysroot -fPIC -isystem /home/user/Downloads/android-ndk-r16b/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security  -std=c++11 -Wno-error=format-security -fpermissive -fopenmp -Og -fopenmp -O0  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /home/user/Downloads/android-ndk-r16b/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -L/home/user/git/project/app/src/main/cpp/libs -laltumfacerecognizesdk_android -laltumfacedetectsdk_android ../../../../src/main/jniLibs/armeabi-v7a/libopencv_java.so /home/user/git/project/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_core.a /home/user/git/project/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_highgui.a /home/user/git/project/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_imgproc.a /home/user/git/project/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_video.a /home/user/Downloads/android-ndk-r16b/platforms/android-23/arch-arm/usr/lib/liblog.so ../../../../src/main/cpp/libs/libgnustl_shared.so -lncnn -lboost_filesystem-clang-mt-a32-1_66 -lboost_system-clang-mt-a32-1_66 /home/user/git/project/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_androidcamera.a /home/user/git/project/OpenCV-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibjpeg.a /home/user/git/project/OpenCV-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibpng.a /home/user/git/project/OpenCV-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibtiff.a /home/user/git/project/OpenCV-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibjasper.a /home/user/git/project/OpenCV-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libIlmImf.a /home/user/git/project/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_imgproc.a /home/user/git/project/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_core.a -lz -ldl -lm -llog -ldl -lm -llog -latomic -lm "/home/user/Downloads/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libsupc++.a" "/home/user/Downloads/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_shared.so" && :
  ./boost/system/system_error.hpp:31: error: undefined reference to 'std::runtime_error::runtime_error(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)'
  ./boost/system/system_error.hpp:31: error: undefined reference to 'std::runtime_error::runtime_error(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)'
  /home/user/Downloads/android-ndk-r16b/sources/cxx-stl/llvm-libc++/include/stdexcept:136: error: undefined reference to 'std::logic_error::logic_error(char const*)'
  ./boost/system/system_error.hpp:31: error: undefined reference to 'std::runtime_error::runtime_error(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)'
  libs/filesystem/src/path.cpp:897: error: undefined reference to 'std::__ndk1::locale::locale(char const*)'
  /home/user/Downloads/android-ndk-r16b/sources/cxx-stl/llvm-libc++/include/__locale:212: error: undefined reference to 'std::__ndk1::locale::use_facet(std::__ndk1::locale::id&) const'
  libs/filesystem/src/path.cpp:914: error: undefined reference to 'std::__ndk1::locale::~locale()'
  libs/filesystem/src/path.cpp:914: error: undefined reference to 'std::__ndk1::codecvt<wchar_t, char, mbstate_t>::id'
  libs/filesystem/src/path.cpp:897: error: undefined reference to 'std::__ndk1::locale::locale(char const*)'
  libs/filesystem/src/path.cpp:947: error: undefined reference to 'std::__ndk1::locale::locale(std::__ndk1::locale const&)'
  libs/filesystem/src/path.cpp:897: error: undefined reference to 'std::__ndk1::locale::locale(char const*)'
  libs/filesystem/src/path.cpp:948: error: undefined reference to 'std::__ndk1::locale::operator=(std::__ndk1::locale const&)'
  libs/filesystem/src/path.cpp:950: error: undefined reference to 'std::__ndk1::locale::~locale()'
  libs/filesystem/src/path.cpp:914: error: undefined reference to 'std::__ndk1::locale::~locale()'
  libs/filesystem/src/path.cpp:914: error: undefined reference to 'std::__ndk1::locale::~locale()'
  /home/user/Downloads/android-ndk-r16b/sources/cxx-stl/llvm-libc++/include/stdexcept:146: error: undefined reference to 'std::logic_error::logic_error(char const*)'
  collect2: error: ld returned 1 exit status
  ninja: build stopped: subcommand failed.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

I had tried to use -DANDROID_STL=gnu_shared or -DANDROID_STL=gnu_static and got the same result. Spent a day on it, I still cannot make it compilable.

In CMakeList:

add_library(lib_opencv SHARED IMPORTED)
add_library(lib_gnustl_shared SHARED IMPORTED)

set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java.so)
set_target_properties(lib_gnustl_shared PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/cpp/libs/libgnustl_shared.so)
target_link_libraries( # Specifies the target library.
                   native-lib
                   lib_opencv
                   # Links the target library to the log library
                   # included in the NDK.
                   ${OpenCV_LIBS}
                   ${log-lib}
                   boost_filesystem-clang-mt-a32-1_66
                   boost_system-clang-mt-a32-1_66
                   lib_gnustl_shared
                    )

In gradle:

        externalNativeBuild {
        cmake {
            arguments "-DANDROID_TOOLCHAIN=gcc","-DANDROID_ARM_NEON=TRUE" ,"-DANDROID_STL_FORCE_FEATURES=OFF", "-DANDROID_STL=gnustl_shared"
            cppFlags "-std=c++11", "-Wno-error=format-security", "-fpermissive", "-fopenmp", "-Og"
        }
holopekochan
  • 595
  • 2
  • 10
  • 18
  • _"I had tried to use -DANDROID_STL=gnu_shared"_ I'm guessing that you meant `gnustl_shared`, and not `gnu_shared`(?). In any case, `gnustl` is deprecated starting with NDK r16, and you should switch to `libc++`. – Michael Mar 09 '18 at 12:09
  • when I used c++_shared to compile, it cannot find vector header. cannot #include , which cause no such file error – holopekochan Mar 10 '18 at 03:46

3 Answers3

7

Your boost library was built with libc++ (you can tell because it has references to std::__ndk1::* rather than just std::*). You need to use libc++, not gnustl.

Dan Albert
  • 10,079
  • 2
  • 36
  • 79
  • is there a way using both c++_shared and gnustl_shared together? Because after changing to c++_shared, I have another error message come out error: undefined reference to Draw::GetRectangles(cv::Mat const&, std::__ndk1::vector >& – holopekochan Mar 10 '18 at 02:58
  • No. Your app must use only one STL. That second error looks like a different issue and you should ask a new question for it. – Dan Albert Mar 10 '18 at 20:45
  • Use `arguments "-DANDROID_STL=c++_shared"`, the gnustl is obsolete now – Amarghosh Dec 27 '18 at 22:57
1

you may miss or forgot the Application.mk file, for me I delete it by mistake, then I got this compile error:

/home/lingyunxiao/dev/GifskiAndroid/gifski/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/gifskiad//home/lingyunxiao/dev/GifskiAndroid/gifski/src/main/cpp/gifski/GifskiJniApi.o: In function `Java_com_lingyunxiao_gifski_GifskiJniApi_gifskiNew':
/home/lingyunxiao/dev/GifskiAndroid/gifski/src/main/cpp/gifski/GifskiJniApi.cpp:45: undefined reference to `std::__ndk1::to_string(int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/home/lingyunxiao/dev/android-sdk/ndk/21.1.6352462/build/core/build-binary.mk:725: /home/lingyunxiao/dev/GifskiAndroid/gifski/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/libgifskiad.so] Error 1

I rollback that file the error gone, this are the content of the mk file:

APP_STL := c++_static
VinceStyling
  • 3,707
  • 3
  • 29
  • 44
0

I had a similar problem but had only changed source code, not the toolchain.

I started getting

/home/myuser/Android/Sdk/ndk/22.0.7026061/sources/cxx-stl/llvm-libc++/include/__locale:887:44: error: implicit instantiation of undefined template 'std::__ndk1::ctype<char *>'
    return use_facet<ctype<_CharT> >(__loc).tolower(__c);
                                           ^

only when compiling on Android, whereas the code compiled fine on Linux and Windows.

As it turns out, when trying to make my code more protable and standard, i had replaced:

char desc[100];
..
if (strcasecmp(desc,"recorddeleted") == 0)

with

char desc[100];
std::locale loc;
..
if (strncmp(std::tolower(desc,loc), "sometext", 13) == 0)

which fails on android. FYI i'm using APP_STL := c++_shared.

vesperto
  • 804
  • 1
  • 6
  • 26