undefined reference to __cxa_allocate_exception

4,811 views
Skip to first unread message

mk.hapless

unread,
May 23, 2012, 2:47:35 PM5/23/12
to android-...@googlegroups.com
Hi,

I've added a library to /external/.  It uses STL and exceptions.

I'm trying to add a small command line utility to test the library.  When I try to build the module, I get undefined symbols from the library:

error: undefined reference to '__cxa_allocate_exception'
error: undefined reference to '__cxa_throw'
error: undefined reference to '__gxx_personality_v0'
etc.


include $(CLEAR_VARS)
LOCAL_MODULE := wavsender
LOCAL_MODULE_TAGS := eng
LOCAL_CFLAGS += -fexceptions -D_GNU_SOURCE -D_REENTRANT -DDEFINE_LITTLE_ENDIAN -DDEFINE_TRACE -DPLATFORM_ANDROID
LOCAL_CPPFLAGS += -Wno-non-virtual-dtor 
LOCAL_SRC_FILES += WavSender.cpp
LOCAL_SHARED_LIBRARIES := libc libcutils libnetutils
LOCAL_STATIC_LIBRARIES := libcutils libopenhome
LOCAL_STATIC_LIBRARIES += libopenhome libgtest libgabi++ libstlport_static libstdc++ 
LOCAL_LDLIBS += -lstdc++ 
include external/stlport/libstlport.mk
include $(BUILD_EXECUTABLE)

(sorry for the mess, just trying to get it to work ...)

androidabi-g++ comes out as:

prebuilt/darwin-x86/toolchain/arm-linux-androideabi-4.4.x/bin/arm-linux-androideabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/wavsender_intermediates/LINKED/wavsender -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -llog -lcutils -lnetutils -lc -lstdc++ -lm  out/target/product/generic/obj/lib/crtbegin_dynamic.o    out/target/product/generic/obj/EXECUTABLES/wavsender_intermediates/ohSongcast/WavSender/WavSender.o              out/target/product/generic/obj/STATIC_LIBRARIES/liblog_intermediates/liblog.a out/target/product/generic/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a out/target/product/generic/obj/STATIC_LIBRARIES/libopenhome_intermediates/libopenhome.a out/target/product/generic/obj/STATIC_LIBRARIES/libopenhome_intermediates/libopenhome.a out/target/product/generic/obj/STATIC_LIBRARIES/libgtest_intermediates/libgtest.a out/target/product/generic/obj/STATIC_LIBRARIES/libgabi++_intermediates/libgabi++.a out/target/product/generic/obj/STATIC_LIBRARIES/libstlport_static_intermediates/libstlport_static.a out/target/product/generic/obj/STATIC_LIBRARIES/libstdc++_intermediates/libstdc++.a   -Wl,-z,noexecstack -Wl,--icf=safe -Wl,--fix-cortex-a8   -Wl,--no-undefined  prebuilt/darwin-x86/toolchain/arm-linux-androideabi-4.4.x/bin/../lib/gcc/arm-linux-androideabi/4.4.3/armv7-a/libgcc.a out/target/product/generic/obj/lib/crtend_android.o

What am I doing wrong?  I can't figure out how to get rid of the undefined symbols ...

thanks for any help

Ron M

unread,
May 24, 2012, 1:37:28 AM5/24/12
to android-...@googlegroups.com
What libc libraries did you previously try to compile this against?
Did it work with glibc? eglibc? uLibc ... ?

I suspect it has to do with your flags and bionic, but I think it
would be informative to know whether that port of you worked on other
configurations - and which ones.
> --
> You received this message because you are subscribed to the "Android
> Building" mailing list.
> To post to this group, send email to android-...@googlegroups.com
> To unsubscribe from this group, send email to
> android-buildi...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-building?hl=en

mk.hapless

unread,
May 24, 2012, 4:37:08 PM5/24/12
to Android Building
libgcc

what I've figured out is, if I link against libstc++.a and libsupc++.a
I can get the link errors to go away.

however, exception handling doesn't work - when an exception is thrown
the program gets a segmentation fault. (runs fine using gnu on a PC)

I've reduced the code down to a simple test case (below). I can get
this built and installed into /system/bin. But instead of the third
printf() coming out I get a segmentation fault.

does anyone know why this is?

thanks

--
#include <stdio.h>
#include <map>

class MyException {
private:
const char* message;
std::map<int,int> table;
public:
MyException (const char *m) {
message = m;
};
const char* getMessage() {
return message;
};
};

class TestException {
public:
static int isok (int i) {
if (i == 0) {
throw new MyException ("value is zero!");
} else if (i < 0) {
return 0;
} else {
return 1;
}
};
};

int main (int argc, char* argv[]) {
try {
printf ("isok(1): %d\n", TestException::isok(1));
printf ("isok(-1): %d\n", TestException::isok(-1));
printf ("isok(0): %d\n", TestException::isok(0));
} catch (MyException *e) {
printf ("exception: %s\n", e->getMessage());
}
return 0;
}

Jean-Baptiste Queru

unread,
May 24, 2012, 4:41:44 PM5/24/12
to android-...@googlegroups.com
Within the platforn, STL and exception support are tricky and best
avoided (and that's quite an understatement).

JBQ
> --
> You received this message because you are subscribed to the "Android Building" mailing list.
> To post to this group, send email to android-...@googlegroups.com
> To unsubscribe from this group, send email to
> android-buildi...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-building?hl=en



--
Jean-Baptiste M. "JBQ" Queru
Software Engineer, Android Open-Source Project, Google.

Questions sent directly to me that have no reason for being private
will likely get ignored or forwarded to a public forum with no further
warning.

mk.hapless

unread,
May 25, 2012, 11:18:16 AM5/25/12
to Android Building
yes, I understand this now :)

unfortunately, I've got an existing library with over 4400 std:: calls
and over 1500 throws.

so it's either rewrite all that stuff, or face the trickiness of
getting the existing library to link.

I've found this incantation works for my test program:

include $(CLEAR_VARS)
LOCAL_MODULE := ttest
LOCAL_MODULE_TAGS := eng
LOCAL_CFLAGS := -fexceptions \
-I prebuilt/ndk/android-ndk-r6/sources/cxx-stl/gnu-libstdc++/
include \
-I prebuilt/ndk/android-ndk-r6/sources/cxx-stl/gnu-libstdc++/libs/
armeabi-v7a/include
LOCAL_SRC_FILES := test.cpp
LOCAL_LDFLAGS := \
prebuilt/ndk/android-ndk-r6/sources/cxx-stl/gnu-libstdc++/libs/
armeabi-v7a/libstdc++.a \
prebuilt/darwin-x86/toolchain/arm-linux-androideabi-4.4.x/arm-
linux-androideabi/lib/armv7-a/libsupc++.a
include $(BUILD_EXECUTABLE)

but it's a somewhat hackery and not sure how I'd make it work for any
platform / target combination ...

is there some standard way to pull in those headers and libraries?

Jean-Baptiste Queru

unread,
May 25, 2012, 11:22:24 AM5/25/12
to android-...@googlegroups.com
There's explicitly no standard way to do that from a platform app,
because it creates some licensing issues that we want to avoid.

JBQ
Reply all
Reply to author
Forward
0 new messages