我正在尝试使用 NDK 5 full C++ gnuSTL:
CPLUSPLUS-SUPPORT.html
声明:
The NDK toolchain supports C++ exceptions, since NDK r5, however all C++ sources are compiled with -fno-exceptions support by default, for compatibility reasons with previous releases.
To enable it, use the '-fexceptions' C++ compiler flag. This can be done by adding the following to every module definition in your Android.mk:
LOCAL_CPPFLAGS += -fexceptions
More simply, add a single line to your Application.mk, the setting will automatically apply to all your project's NDK modules:
APP_CPPFLAGS += -fexceptions
sources/cxx-STL/gnu-libstdc++/README
状态:
This directory contains the headers and prebuilt binaries for the GNU libstdc++-v3 C++ Standard Template Library implementation.
These are generated from the toolchain sources by the rebuild-all-prebuilt.sh script under build/tools.
To use it, define APP_STL to 'gnustl_static' in your Application.mk. See docs/CPLUSPLUS-SUPPORT.html for more details.
This implementation fully supports C++ exceptions and RTTI.
但是所有使用异常的尝试都失败了。 http://www.crystax.net/android/ndk-r4.php 上存在替代 NDK .使用该 NDK 中的 hello-jni 示例不起作用。使用
创建Application.xml
后,与 NDK 5 的兼容有效
APP_STL := gnustl_static
将 APP_STL 设置为 gnuSTL_static
也会自动启用 -frtti
和 -fexceptions
。但它和我自己的实验一样可怕地死去。
我已经设法获得了一个对我来说崩溃的代码的最小示例:
try {
__android_log_write(ANDROID_LOG_DEBUG,"foobar","trhown!");
throw "Wrong object type.";
} catch (char* b) {
__android_log_write(ANDROID_LOG_DEBUG,"foobar","catched!");
}
是我遗漏了什么还是 README
和 CPLUSPLUS-SUPPORT.html
中的语句完全错误?
最佳答案
事实证明,异常有效,但前提是该异常是从 std::exception 继承的。在我的情况下,异常层次结构并不总是包括破坏捕获/抛出的 std::exception 。奇怪的是,当为 x86/Mac OS 编译时,抛出字符串作为异常有效。我通过修改我使用的异常解决了我的问题。
关于c++ - Android NDK R5 和对 C++ 异常的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4663291/