Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK R5 and support of C++ exception

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?

like image 538
plaisthos Avatar asked Jan 11 '11 22:01

plaisthos


2 Answers

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.

like image 97
plaisthos Avatar answered Sep 19 '22 10:09

plaisthos


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.

The STLport supplied is not usable together with exceptions or RTTI.

Note that it may be necessary to clean the build objects when swapping between STL implementations.

like image 39
grrussel Avatar answered Sep 21 '22 10:09

grrussel