我正在尝试在使用std::bad_function_call
函数的Android Studio项目中构建本机库。
这是我用来构建本机模块的CMakeLists.txt
文件:
cmake_minimum_required(VERSION 3.4.1)
include_directories(src/main/cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -D_LIBCPP_ABI_UNSTABLE")
add_library(
MyLib
SHARED
src/main/cpp/test.cpp
)
我的test.cpp
包含以下代码:
void SomeClass::SomeMethod() {
std::function<int()> f = nullptr;
try {
f();
} catch (const std::bad_function_call& e) {
}
}
带有相应的build.gradle部分:
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_static", "-DANDROID_NDK_ROOT=${android.ndkDirectory}"
}
}
如果将_LIBCPP_ABI_UNSTABLE
标志传递给编译器,则会出现链接错误:
Execution failed for task ':runtime:externalNativeBuildRelease'.
> Build command failed.
Error while executing process C:\android-sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build C:\work\android-runtime\test-app\runtime\.externalNativeBuild\cmake\release\armeabi-v7a --target MyLib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\release\obj\armeabi-v7a\libMyLib.so
FAILED: cmd.exe /C "cd . && C:\android-sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi17 --gcc-toolchain=C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 -fPIC --sysroot C:/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 -mfpu=vfpv3-d16 -fno-addrsig -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -fno-rtti -D_LIBCPP_ABI_UNSTABLE -Oz -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libMyLib.so -o ..\..\..\..\build\intermediates\cmake\release\obj\armeabi-v7a\libMyLib.so CMakeFiles/MyLib.dir/src/main/cpp/test.cpp.o ../../../../src/main/libs/armeabi-v7a/abc.a -llog -latomic -lm && cd ."
C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/functional:0: error: undefined reference to 'vtable for std::__ndk1::bad_function_call'
C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: the vtable symbol may be undefined because the class is missing its key function
C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/functional:0: error: undefined reference to 'std::__ndk1::bad_function_call::~bad_function_call()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
如果我删除了_LIBCPP_ABI_UNSTABLE
标志:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
然后该项目编译正常。
所以我的问题是启用libc++ unstable abi
并在本机模块中使用bad_function_call
方法的正确方法是什么?
注意:我正在使用Android NDK 19。