[FIXED] Error Undefined reference to 'std::__ndk1::locale::~locale()'

Issue

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"
        }

Solution

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.

Answered By – Dan Albert

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published