c++ - Android NDK R5 和对 C++ 异常的支持

标签 c++ exception android-ndk

我正在尝试使用 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!");
}

是我遗漏了什么还是 READMECPLUSPLUS-SUPPORT.html 中的语句完全错误?

最佳答案

事实证明,异常有效,但前提是该异常是从 std::exception 继承的。在我的情况下,异常层次结构并不总是包括破坏捕获/抛出的 std::exception 。奇怪的是,当为 x86/Mac OS 编译时,抛出字符串作为异常有效。我通过修改我使用的异常解决了我的问题。

关于c++ - Android NDK R5 和对 C++ 异常的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4663291/

相关文章:

c++ - dynamic_cast 到具有未知模板参数的派生类型

java - 抛出异常

从 C++ JNI NDK 函数调用两次的 Java 方法

c++ - 在自建android中包含C++共享库。启动 ndk-build 时出错

c++ - 无效标量十六进制值 0x8000000 及以上

c++ - 错误 : expected constructor, 析构函数,或 ';' token 之前的类型转换?

c++ - 在 C++ 中捕获段错误或任何其他错误/异常/信号,就像在 Java 中捕获异常一样

c# - 处理异步套接字回调中的异常

android - 以编程方式重新混合 mp4 并使用 ffmpeg 更改分辨率

c++ - 如何在 C++ 中编写缓存友好的多态代码?