20

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?

Graeme
  • 25,714
  • 24
  • 124
  • 186
  • __cxa_*** strikes me as GNU specific. Are you sure that you don't accidentally include a non-NDK header (such as from /usr/include)? – sehe Feb 10 '12 at 10:50
  • 2
    a) "Possible Duplicate of" link is 10 months older than this question. b) Question is not a true question, instead a Tutorial on common linking / compilation problems. c) Link does NOT answer this question as this problem is *specifically* an Android NDK issue not a generic C++ linking problem. – Graeme Jul 04 '14 at 14:27
  • @Graeme: how this is any Android specific or why it is any relevant how old the canonical is is way beyond my league. The fact is that you lack the basics about C/C++, especially linking. That really is explained in the canonical answer. If you have C++ **and** Android issues, ask them in separate questions, and in that case it is even too broad. – László Papp Jul 05 '14 at 02:45
  • @FinalContest - I'm not an expert on Linking it's true. But it wasn't that I didn't understand linking but that I didn't know which libraries were available on Android devices. You'll know that specifying an STL using the NDK is different than using a command line compiler. You may also notice none of that information in my answer is available in the linked question. – Graeme Jul 07 '14 at 13:55
  • @Graeme: as I already wrote, your question was too broad and/or unclear then. That means you still need to improve your question to be a valid question on Stack Overflow. You cannot ask about linking _and_ Android and who knows what else. They are different topics on their own. You need tpresent some minimal understanding. Moreover, this is not only broad and unclear, but also off-topic by definition: `Is there another library available to the NDK?` Please refer to the help center how to ask a good question. – László Papp Jul 07 '14 at 14:00
  • This question asks a self contained, targeted and specific question based on a real world programming problem. It is answered with a self contained explanation which has helped at least 24 people. It seems to perfectly fit with the definition of a "Good Question". This question isn't "what is linking" but instead "I'm using the wrong stl and I'm unsure which others are available that I can use" which is perfectly valid. – Graeme Jul 07 '14 at 14:42

5 Answers5

26

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
Graeme
  • 25,714
  • 24
  • 124
  • 186
3

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++

osexp2000
  • 2,910
  • 30
  • 29
  • 4
    Could you give some detail on how this flag fixes the problem? And why? Think of your answer as something a lot of people will read, not just a one off. – starmole Jun 24 '14 at 02:57
  • You should really not provide incorrect comments as answers. You do not even explain the issue. – László Papp Jul 05 '14 at 02:35
3

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

In Android's Application.mk this would be:

APP_CPPFLAGS := -frtti
Community
  • 1
  • 1
NuSkooler
  • 5,391
  • 1
  • 34
  • 58
1

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.

TimWagaman
  • 980
  • 1
  • 10
  • 31
0

for me that meant adding -fno-rrti and -fno-exceptions then getting rid of "throw char*" in the code which took care of both.

user3048370
  • 71
  • 1
  • 1