Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linker errors in Android NDK (undefined reference to `__cxa_end_cleanup')

I'm getting this output after adding in a set of code from a colleague:

./obj/local/armeabi/objs/jniWrapper/native.o: In function `_Vector_base':
D:/opt/android-ndk/sources/cxx-stl/stlport/stlport/stl/_vector.h:73: undefined reference to `__cxa_end_cleanup'
./obj/local/armeabi/objs/jniWrapper/native.o:(.ARM.extab.text._ZNSt6vectorIhSaIhEEC1ERKS1_[std::vector<unsigned char, std::allocator<unsigned char> >::vector(std::vector<unsigned char, std::allocator<unsigned char> > const&)]+0x0): undefined reference to `__gxx_personality_v0'
./obj/local/armeabi/objs/jniWrapper/native.o: In function `std::__node_alloc::deallocate(void*, unsigned int)':
D:/opt/android-ndk/sources/cxx-stl/stlport/stlport/stl/_alloc.h:161: undefined reference to `__cxa_end_cleanup'
./obj/local/armeabi/objs/jniWrapper/native.o:(.ARM.extab.text._ZNSt4priv12_String_baseIcSaIcEED2Ev[std::priv::_String_base<char, std::allocator<char> >::~_String_base()]+0x0): undefined reference to `__gxx_personality_v0'
./obj/local/armeabi/objs/jniWrapper/native.o: In function `basic_string':
D:/opt/android-ndk/sources/cxx-stl/stlport/stlport/stl/_string.c:643: undefined reference to `__cxa_end_cleanup'

This is caused because I don't have access to the correct STL.

To my knowledge there are only three I can choose from (stlport_static, stlport_shared, system) as set by APP_STL := stlport_static in Application.mk.

Is there another library available to the NDK?

like image 670
Graeme Avatar asked Feb 10 '12 10:02

Graeme


4 Answers

After reading android-ndk/docs/CPLUSPLUS-SUPPORT.html I found that there a couple more libraries that I can link to:

             C++       C++   Standard
          Exceptions  RTTI    Library

system        no       no        no
gabi++        no      yes        no
stlport       no      yes       yes
gnustl       yes      yes       yes

This stops my linker errors (and pushes the build onto a new set of errors :))

Application.mk

APP_STL := gnustl_static
like image 167
Graeme Avatar answered Nov 18 '22 07:11

Graeme


Take a look here: Linux C++: Linker is outputting strange errors.

In Android's Application.mk this would be:

APP_CPPFLAGS := -frtti
like image 43
NuSkooler Avatar answered Nov 18 '22 07:11

NuSkooler


You can fix this problem by adding the compiler option -lsupc++.

Edited: The reason: your code is using C++ exception mechanism which compiler automatically generate try/catch/finally block hidden code which in turn call __cxa_end_cleanup somewhere. lsupc++ means link to libsupc++.a

Another way to resolve this problem is add -fno-exceptions option to gcc which obviously means disable exception handler mechanism.

BTW, you should also add -fno-rtti to avoid other maybe-encountered compilation error, this is because all android's C++ class is compiled without dynamic type info in class memory layout.

In a word, you shoud use one of these option combos: 1. -fno-rtti -fno-exceptions 2. -fno-rtti -lsupc++

like image 3
osexp2003 Avatar answered Nov 18 '22 08:11

osexp2003


In my case, the error undefined reference to __cxa_end_cleanup shows up when I add -fexceptions to the compiler options. When removing that option, the undefined ref goes away, but that means you have to clear your code from exception statements.

like image 1
user2054803 Avatar answered Nov 18 '22 07:11

user2054803