Android NDK always records runtime dependency against stdc++
Regardless of the CMAKE_ANDROID_STL_TYPE
chosen (e.g., c++_static
), all shared libraries and executables built with the Android NDK are linked against stdc++.
I'm attaching a simple test case that reproduces the issue (change NDK_ROOT
in run.sh
to point to the NDK and then run run.sh
):
CMakeLists.txt
run.sh
test_lib.cc
test.cc
Newer versions of clang support the nostdlib++
flag, but the latest stable NDK (16b) does not have a version of clang with that flag: https://reviews.llvm.org/D35780
Also see this NDK issue: https://github.com/android-ndk/ndk/issues/105
As a workaround, you can use the C compiler to link instead of the C++ compiler in your project:
string(REPLACE "CMAKE_CXX_COMPILER" "CMAKE_C_COMPILER" CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
string(REPLACE "CMAKE_CXX_COMPILER" "CMAKE_C_COMPILER" CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
link_libraries(m)
Another option is to set -nodefaultlibs
and manually add in the necessary libs: https://github.com/android-ndk/ndk/issues/105#issuecomment-327409972