Android NDK R5 和 C++ 异常支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4663291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android NDK R5 and support of C++ exception
提问by plaisthos
I am trying to use the NDK 5 full C++ gnustl:
我正在尝试使用 NDK 5 full C++ gnustl:
The CPLUSPLUS-SUPPORT.html
states:
的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
NDK 工具链支持 C++ 异常,自 NDK r5 起,但所有 C++ 源代码默认编译时都支持 -fno-exceptions,这是出于与先前版本兼容的原因。
要启用它,请使用“-fexceptions”C++ 编译器标志。这可以通过将以下内容添加到 Android.mk 中的每个模块定义来完成:
LOCAL_CPPFLAGS += -fexceptions
更简单地,在 Application.mk 中添加一行,该设置将自动应用于您项目的所有 NDK 模块:
APP_CPPFLAGS += -fexceptions
sources/cxx-stl/gnu-libstdc++/README
states:
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.
此目录包含 GNU libstdc++-v3 C++ 标准模板库实现的头文件和预构建的二进制文件。
这些是通过 build/tools 下的 rebuild-all-prebuilt.sh 脚本从工具链源生成的。
要使用它,请在 Application.mk 中将 APP_STL 定义为“gnustl_static”。有关更多详细信息,请参阅 docs/CPLUSPLUS-SUPPORT.html。
此实现完全支持 C++ 异常和 RTTI。
But all attempts using exceptions fail. An alternative NDK exists on http://www.crystax.net/android/ndk-r4.php. Using the the hello-jni example from that NDK does not work. Compliation with NDK 5 works after creating an Application.xml
with
但是所有使用异常的尝试都失败了。http://www.crystax.net/android/ndk-r4.php 上存在替代 NDK 。使用该 NDK 中的 hello-jni 示例不起作用。创建与 NDK 5 的Application.xml
兼容工作
APP_STL := gnustl_static
Setting APP_STL to gnustl_static
also automatically enables -frtti
and -fexceptions
. But it dies the same horrific death as my own experiments.
将 APP_STL 设置为gnustl_static
也自动启用-frtti
和-fexceptions
。但它和我自己的实验一样可怕的死亡。
I have managed to get a minimal example of code that is crashing for me:
我设法得到了一个让我崩溃的最小代码示例:
try {
__android_log_write(ANDROID_LOG_DEBUG,"foobar","trhown!");
throw "Wrong object type.";
} catch (char* b) {
__android_log_write(ANDROID_LOG_DEBUG,"foobar","catched!");
}
Am I am missing something or is the statement in the README
and CPLUSPLUS-SUPPORT.html
just plain wrong?
就是我失去了一些东西或者是在陈述README
和CPLUSPLUS-SUPPORT.html
只是简单的错误?
采纳答案by plaisthos
It turns out that exceptions work but only if the exception are inherited from std::exception. In my case the exception hierarchy did not always include std::exception which broke the catch/throw. Curiously the throwing strings as exceptions works when compiled for x86/Mac OS. I have fixed my problem by modifying the exceptions I use.
事实证明,异常有效,但前提是异常是从 std::exception 继承的。在我的情况下,异常层次结构并不总是包含破坏捕获/抛出的 std::exception。奇怪的是,当为 x86/Mac OS 编译时,作为异常的抛出字符串有效。我通过修改我使用的异常解决了我的问题。
回答by grrussel
The NDK-r5 tools support the use of exceptions and RTTI in C++ code. The use of an STL other than the GNU STL as a static library is not, however, supported, in the presence of RTTI or exceptions.
NDK-r5 工具支持在 C++ 代码中使用异常和 RTTI。但是,在存在 RTTI 或异常的情况下,不支持使用 GNU STL 以外的 STL 作为静态库。
The STLport supplied is not usable together with exceptions or RTTI.
提供的 STLport 不能与异常或 RTTI 一起使用。
Note that it may be necessary to clean the build objects when swapping between STL implementations.
请注意,在 STL 实现之间交换时可能需要清理构建对象。
回答by Kervala
I have a similar problem using JNI. All exceptions thrown from a JNI method are causing a SIGILL error under Android 1.6 and 2.1. It's working fine under Android 2.2+
我在使用 JNI 时遇到了类似的问题。在 Android 1.6 和 2.1 下,从 JNI 方法抛出的所有异常都会导致 SIGILL 错误。它在 Android 2.2+ 下运行良好
See my issue (please don't hesitate to vote for it or post a comment) :
看到我的问题(请不要犹豫,投赞成票或发表评论):
http://code.google.com/p/android/issues/detail?id=20176
http://code.google.com/p/android/issues/detail?id=20176
So, currently, Android 1.6 and 2.1 don't support exceptions from JNI methods with the last NDK.
因此,目前,Android 1.6 和 2.1 不支持来自最后一个 NDK 的 JNI 方法的异常。
Perhaps could it be fixed in a future NDK release...
也许它可以在未来的 NDK 版本中修复......
回答by Alex Miller
As far as I know, the Android NDK has never supported exceptions. libstdc++ itself does support exceptions, but when compiled for android, exception support is turned off (grep for "-fno-exceptions").
据我所知,Android NDK 从来不支持异常。libstdc++ 本身确实支持异常,但是当为 android 编译时,异常支持被关闭(grep 表示“-fno-exceptions”)。
See this threadon the android ndk mailing list.
在 android ndk 邮件列表上查看此线程。