Android的NDK R5和支持的C ++异常 [英] Android NDK R5 and support of C++ exception

查看:169
本文介绍了Android的NDK R5和支持的C ++异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用NDK 5个完整的C ++ gnustl:

CPLUSPLUS-的support.html 规定:


  

NDK的工具链支持C ++异常,因为NDK R5,但是所有的C ++
  来源默认情况下使用-fno-异常支持编译的,对于
  兼容性的原因与previous版本。


  
  

要启用它,使用'-fexceptions'C ++编译器标志。这是可以做到
  通过添加以下到您的Andr​​oid.mk每个模块定义:

  LOCAL_CPPFLAGS + = -fexceptions


  
  

更简单地说,一行添加到您的Application.mk,设置将
  自动适用于所有项目的NDK模块:

  APP_CPPFLAGS + = -fexceptions


来源/ CXX-STL / GNU-的libstdc ++ / README 规定:


  

该目录包含了头和prebuilt二进制
  GNU的libstdc ++ - v3的C ++标准模板库执行


  
  

这些都是由重建,清一色prebuilt.sh工具链生成的来源
  在构建/工具脚本。


  
  

要使用它,在你的Application.mk定义APP_STL为gnustl_static。
  见文档/ CPLUSPLUS-的support.html了解更多详情。


  
  

这实现完全支持C ++异常和RTTI。


不过,使用异常的所有尝试都失败了。另一种NDK存在于 http://www.crystax.net/android/ndk-r4.php 。使用从NDK不工作的HELLO-JNI的例子。 Compliation与创建后NDK的5部作品的的Application.xml

  APP_STL:= gnustl_static

设置APP_STL到 gnustl_static 也将自动启用 -frtti -fexceptions 。但它死一样可怕的死亡作为我自己的实验。

我设法得到code,它崩溃,我的一个小例子:

  {尝试
    __android_log_write(ANDROID_LOG_DEBUG,foobar的,trhown!);
    扔错对象类型。
}赶上(CHAR * B){
    __android_log_write(ANDROID_LOG_DEBUG,foobar的,逮住!);
}

就是我失去了一些东西或者是在 README CPLUSPLUS-的support.html 只是简单的错误语句?


解决方案

原来,例外工作,但只有在例外情况是自std ::继承例外。在我的情况下的异常层次结构始终没包括的std ::例外,这打破了捕获/扔。奇怪的是编译为86 /的Mac OS当投掷字符串作为例外的作品。我已经通过修改我用的是固定例外我的问题。

I am trying to use the NDK 5 full C++ gnustl:

The CPLUSPLUS-SUPPORT.html states:

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 states:

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.

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

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.

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?

解决方案

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.

这篇关于Android的NDK R5和支持的C ++异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆