13

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?

plaisthos
  • 6,255
  • 6
  • 35
  • 63

4 Answers4

9

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.

plaisthos
  • 6,255
  • 6
  • 35
  • 63
  • 3
    The use of types not derived from std::exception in C++ is quite legal, so its not surprising that it works on OSX. The codebase I work on throws integers as exceptions, and works in Android. – grrussel Jan 20 '11 at 18:21
  • 1
    The ndk comes with individual versions of the stdc++, one if which provides the gnu implementation: when I link with /android-ndk-r5b/sources/cxx-stl/gnu-libstdc++/libs/armeabi/libstdc++.a explicitly, the problem is gone. But I'm sure there is an easier way. – paniq Aug 23 '11 at 14:27
7

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.

grrussel
  • 7,209
  • 8
  • 51
  • 71
  • 1
    The question asks if the NDK documentation statement about support for exceptions is wrong; it is not. – grrussel Aug 23 '11 at 15:41
1

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").

See this thread on the android ndk mailing list.

Alex Miller
  • 448
  • 2
  • 9
  • 2
    Support for exceptions/rtti ist new with version r5. According to changes and the README of the gnustl it should be supported. – plaisthos Jan 12 '11 at 10:22
  • http://developer.android.com/sdk/ndk/index.html still has "C++ Exceptions and RTTI are not supported in the default STL implementation." in the general notes section. Where did you see that they added support for exceptions was enabled in r5? And yes, the standard library implementation that they use does support exceptions, however, when they compile it for android they explicitly disable exceptions. (Or at least have up through r4) – Alex Miller Jan 13 '11 at 07:44
  • Now that I'm re-reading this, when you tried the crystax ndk, did you do the full compilation of libstdc++ with their patch and then link hello-jni against that libstdc++, or did you just use the code in hello-jni and link it against the default libstdc++? The former should give you full exception support; the latter will die horribly. – Alex Miller Jan 13 '11 at 08:00
  • 1
    I clarified the post. But from the information of the NDK both ways should work. THe default libstdc++ won't work but the gnustl should work. – plaisthos Jan 13 '11 at 15:29
1

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+

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

So, currently, Android 1.6 and 2.1 don't support exceptions from JNI methods with the last NDK.

Perhaps could it be fixed in a future NDK release...

Kervala
  • 516
  • 1
  • 7
  • 7