我正在尝试将现有的基于 Android.mk 的构建转换为 CMake,但我在构建带有异常和 rtti 的 CPP 代码时遇到了困难。生成的cpp构建命令默认包含-fno-exceptions
和-fno-rtti
。将它们添加为 ANDROID_CPP_FEATURES
或 cppFlags
只会在默认选项之后将它们附加到构建命令,并且构建仍然失败。
我搜索了很多,除了少数与 Android 上的 CMake 有关的页面外,我从搜索中得到的大部分是旧的 Android.mk 结果。
在任何 CMakeLists.txt 文件中都没有什么特别之处,据我所知,没有任何东西可以添加这些选项。
下面所有未注释的选项均无效。
build.gradle:
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=gcc',
'-DANDROID_PLATFORM=android-19',
// '-DANDROID_STL_FORCE_FEATURES=ON',
'-DANDROID_STL=gnustl_static'
//'-DANDROID_CPP_FEATURES=rtti'
//'-DANDROID_CPP_FEATURES=exceptions',
cppFlags "-fexceptions -frtti"
targets 'library2'
}
}
构建命令中的选项:
<removed content> -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -frtti -fexceptions -frtti -O2 -DNDEBUG -fPIC -MD -MT <additional content>
最佳答案
借助 NDK 工具链 r13b,我在 gradle 中使用了这种组合。通过我关闭的部分是重复 -DANDROID_CPP_FEATURES
覆盖自身,你需要一个空格而不是 exceptions
和 rtti
之间的逗号。
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_PLATFORM=android-19',
'-DANDROID_STL=gnustl_static',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_CPP_FEATURES=exceptions rtti'
}
}
关于Android CMake 构建默认添加 no-exceptions 和 no-rtti,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40000266/