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

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?

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

Take a look here: Linux C++: Linker is outputting strange errors.
In Android's Application.mk this would be:
APP_CPPFLAGS := -frtti

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

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.

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

Related

Android NDK error: undefined reference

There have been some questions similar to mine, but it seems that their solutions don't work for me.
I'm trying to compile dumpsys source code using Android NDK. I have added a couple of lines to Android.mk to include the libraries.
The final Android.mk file looks like this:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
dumpsys.cpp
LOCAL_SHARED_LIBRARIES := \
libutils \
liblog \
libbinders
ANDROID_SRC="my android source directory"
LOCAL_C_INCLUDES := ${ANDROID_SRC}/frameworks/native/include \
${ANDROID_SRC}/system/core/include
#$(warning $(TARGET_C_INCLUDES))
LOCAL_MODULE:= dumpsys
TARGET_ARCH := arm
TARGET_ARCH_ABI := armeabi-v7a
include $(BUILD_EXECUTABLE)
when I execute ndk-build, I get the following errors:
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:144: error: undefined reference to 'android::String16::~String16()'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:240: error: undefined reference to 'android::VectorImpl::finish_vector()'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:241: error: undefined reference to 'android::VectorImpl::~VectorImpl()'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:135: error: undefined reference to 'android::String16::String16()'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:154: error: undefined reference to 'android::String16::String16(android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:166: error: undefined reference to 'android::String16::String16(android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/String16.h:178: error: undefined reference to 'strzcmp16'
/home/mahdi/university/androidsource/system/core/include/utils/StrongPointer.h:143: error: undefined reference to 'android::RefBase::decStrong(void const*) const'
jni/dumpsys.cpp:32: error: undefined reference to 'android::defaultServiceManager()'
jni/dumpsys.cpp:35: error: undefined reference to '__android_log_print'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:224: error: undefined reference to 'android::VectorImpl::VectorImpl(unsigned int, unsigned int)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:224: error: undefined reference to 'android::VectorImpl::VectorImpl(unsigned int, unsigned int)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:245: error: undefined reference to 'android::VectorImpl::operator=(android::VectorImpl const&)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:378: error: undefined reference to 'android::VectorImpl::sort(int (*)(void const*, void const*))'
jni/dumpsys.cpp:49: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:49: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:51: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:51: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:53: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:53: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:66: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
jni/dumpsys.cpp:81: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
jni/dumpsys.cpp:89: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/StrongPointer.h:143: error: undefined reference to 'android::RefBase::decStrong(void const*) const'
jni/dumpsys.cpp:94: error: undefined reference to 'android::aerr'
jni/dumpsys.cpp:94: error: undefined reference to 'android::aout'
collect2: error: ld returned 1 exit status
How should I solve this issue?
Thanks in advance.
I'm working on a similar issue here.
Basically, dumpsys is an AOSP component and intended to be built with the AOSP toolchain. You will need to apply some tweaks to port it to NDK – including stuff from ${ANDROID_SRC} is the first step but not the whole story.
You've successfully included the headers and thus made the compiler happy. Now the linker is complaining because it cannot find the libraries you are linking against. The good news is that they are shared libraries, thus having the libraries around at build time isn't a strict requirement.
The NDK defines a stable API of libraries you can use, which is documented here. liblog is in that list and can be included by adding the following lines to Android.mk:
LOCAL_LDLIBS := \
-llog \
The other two libraries are not part of the stable API. This essentially means that even if your code works on a particular version of Android, it may break on any later version because the API may have changed – you might want to bear this in mind.
Since these libraries are shared, all ld does is check if they actually provide the functions you're using. This question and its accepted answer have instructions for getting rid of the related error messages:
One way is to use something like:
LOCAL_LDFLAGS := -Wl,--unresolved-symbols=ignore-all
However, this will bypass all checks – so if you try to use a function that is indeed absent from the library, ld has no chance of warning you.
The cleaner, but more work-intensive approach, is to provide stub libraries. A stub library is essentially a dummy library which defines the same symbols (functions etc.) as the "real" thing but has no implementation (functions simply return without doing anything). It's enough to make the linker happy, but the libraries are not shipped and their "real" counterparts are used at runtime.
You would need to get the source code for the two libraries, which reside in the following directories: libutils and libbinders
system/core/libutils
frameworks/native/libs/binder
Copy these two dirs into your project's jni dir. Then strip the code down:
* Edit Android.mk, removing all build targets other than BUILD_SHARED_LIBRARY.
* Edit the source code files, replacing all function bodies with a simple return. It doesn't matter what you return, as long as you get the code to compile.
Eventually you will probably need to prevent the stub libraries from getting included in your .apk (I have yet to figure out how to do that).

Undefined Reference to android::LPAPlayer::*

I am having great difficulty with a step in compiling CyanogenMod 11(Android 4.4) for an unsupported device based on an MSM7x27A/Snapdragon S1 platform. When I attempt to build libstagefright, I come up with an undefined reference error to functions in android::LPAPlayer::*, including the constructor. The error I get is:
Copying: /home/dyngar-f/android/android/out/target/common/obj/JAVA_LIBRARIES/mms-common_intermediates/noproguard.classes.jar
frameworks/av/media/libstagefright/AwesomePlayer.cpp:1282: error: undefined reference to 'android::LPAPlayer::LPAPlayer(android::sp<android::MediaPlayerBase::AudioSink> const&, bool&, android::AwesomePlayer*)'
frameworks/av/media/libstagefright/AwesomePlayer.cpp:1306: error: undefined reference to 'android::LPAPlayer::mObjectsAlive'
frameworks/av/media/libstagefright/AwesomePlayer.cpp:1882: error: undefined reference to 'android::LPAPlayer::mObjectsAlive'
collect2: error: ld returned 1 exit statusbuild/core/shared_library.mk:81: recipe for target '/home/dyngar-f/android/android/out/target/product/schS738c/obj/SHARED_LIBRARIES/libstagefright_intermediates/LINKED/libstagefright.so' failed
make: *** [/home/dyngar-f/android/android/out/target/product/schS738c/obj/SHARED_LIBRARIES/libstagefright_intermediates/LINKED/libstagefright.so] Error 1make: *** Waiting for unfinished jobs....
I have searched high and low and I found no mention of this error that leads me to a solution to this problem, and I haven't arrived at a solution myself. Here are the references to this issue I have found from others:
Gist from CMartinBaughman
Mentioned in XDA Thread on this page there is also an answer that doesn't seem to work.
Can anyone guide me to a solution for this issue?
The error is coming from the linker which is unable to find the reference to LPAPlayer which is actually a customization in CyanogenMod sources. From the latest tree, Android.mk for libstagefright is not including LPAPlayer.cpp for all platforms.
To resolve your issue, please try by adding the following files in Android.mk between lines 114-117
#New lines included
ifeq ($(call is-chipset-in-board-platform,msm7x27),true)
LOCAL_SRC_FILES += LPAPlayer.cpp
LOCAL_CFLAGS += -DLEGACY_LPA -DUSE_LPA_MODE
endif
ifeq ($(call is-chipset-in-board-platform,msm7x30),true)
LOCAL_SRC_FILES += LPAPlayer.cpp
LOCAL_CFLAGS += -DLEGACY_LPA -DUSE_LPA_MODE
endif
The platform name may need to be modified to suit the actual name (Please refer to BoardConfig.mk).

compiling libcurl for android mips

i've created a script that compiles curl for android for 3 platforms, well more like 4:
1. armv5
2. armv7
3. x86
4. mips
i do it using this post in SO: enter link description here
for arm and x86 everything works ok (i use ndk toolchains and openssl specific library which i compiled for those same platform beforehand).
the only problematic platform is mips. i DO get the libcrul.a,la and .so BUT when trying to compile the executable curl the linkage fails:
libtool: link: mipsel-linux-android-gcc -O2 -Wno-system-headers -o .libs/curl curl-tool_binmode.o curl-tool_bname.o curl-tool_cb_dbg.o curl-tool_cb_hdr.o curl-tool_cb_prg.o curl-tool_cb_rea.o curl-tool_cb_see.o curl-tool_cb_wrt.o curl-tool_cfgable.o curl-tool_convert.o curl-tool_dirhie.o curl-tool_doswin.o curl-tool_easysrc.o curl-tool_formparse.o curl-tool_getparam.o curl-tool_getpass.o curl-tool_help.o curl-tool_helpers.o curl-tool_homedir.o curl-tool_hugehelp.o curl-tool_libinfo.o curl-tool_main.o curl-tool_metalink.o curl-tool_mfiles.o curl-tool_msgs.o curl-tool_operate.o curl-tool_operhlp.o curl-tool_panykey.o curl-tool_paramhlp.o curl-tool_parsecfg.o curl-tool_setopt.o curl-tool_sleep.o curl-tool_urlglob.o curl-tool_util.o curl-tool_vms.o curl-tool_writeenv.o curl-tool_writeout.o curl-tool_xattr.o ../lib/curl-strtoofft.o ../lib/curl-strdup.o ../lib/curl-rawstr.o ../lib/curl-nonblock.o ../lib/curl-warnless.o -L/usr/local/android-ndk/platforms/android-9/arch-mips/usr/lib -L/home/eyal/devel/openssl/lib ../lib/.libs/libcurl.so -lz
/usr/local/ndk-toolchains/mipsel-linux-android-4.6/bin/../lib/gcc/mipsel-linux-android/4.6/../../../../mipsel-linux-android/bin/ld: warning: libssl.so.1.1.0, needed by ../lib/.libs/libcurl.so, not found (try using -rpath or -rpath-link)
/usr/local/ndk-toolchains/mipsel-linux-android-4.6/bin/../lib/gcc/mipsel-linux-android/4.6/../../../../mipsel-linux-android/bin/ld: warning: libcrypto.so.1.1.0, needed by ../lib/.libs/libcurl.so, not found (try using -rpath or -rpath-link)
../lib/.libs/libcurl.so: undefined reference to `RAND_load_file'
../lib/.libs/libcurl.so: undefined reference to `SSL_CTX_use_certificate'
../lib/.libs/libcurl.so: undefined reference to `X509_load_crl_file'
../lib/.libs/libcurl.so: undefined reference to `SSL_set_fd'
../lib/.libs/libcurl.so: undefined reference to `SSL_set_connect_state'
../lib/.libs/libcurl.so: undefined reference to `SSL_CTX_free'
../lib/.libs/libcurl.so: undefined reference to `X509_free'
there are more 'undefined reference' errors, i spared u...
the library exists, the -L paths are ok, i checked, permission seems to be fine, and again, it works for both arm and x86 for android.
i'm using --host=mipsel-linux-android which might be wrong, but seems like it's the right one, the .so and .a are built so something went alright, but ld for some reason can't locate ssl and crypt once it's requested when trying to create and executable.
Anyone has any idea ? when tryying to use the toolchain manually to compile a file using libssl i was successful!
10x.
../lib/.libs/libcurl.so: undefined reference to `RAND_load_file'
These are OpenSSL functions. Do you have OpenSSL cross compiled for MIPS? If so, is it available?
libtool: link: mipsel-linux-android-gcc -O2 -Wno-system-headers -o .libs/curl curl-tool_binmode.o
curl-tool_bname.o curl-tool_cb_dbg.o curl-tool_cb_hdr.o curl-tool_cb_prg.o curl-tool_cb_rea.o
curl-tool_cb_see.o curl-tool_cb_wrt.o curl-tool_cfgable.o curl-tool_convert.o curl-tool_dirhie.o
curl-tool_doswin.o curl-tool_easysrc.o curl-tool_formparse.o curl-tool_getparam.o
curl-tool_getpass.o curl-tool_help.o curl-tool_helpers.o curl-tool_homedir.o curl-tool_hugehelp.o
curl-tool_libinfo.o curl-tool_main.o curl-tool_metalink.o curl-tool_mfiles.o curl-tool_msgs.o
curl-tool_operate.o curl-tool_operhlp.o curl-tool_panykey.o curl-tool_paramhlp.o
curl-tool_parsecfg.o curl-tool_setopt.o curl-tool_sleep.o curl-tool_urlglob.o curl-tool_util.o
curl-tool_vms.o curl-tool_writeenv.o curl-tool_writeout.o curl-tool_xattr.o ../lib/curl-strtoofft.o
../lib/curl-strdup.o ../lib/curl-rawstr.o ../lib/curl-nonblock.o ../lib/curl-warnless.o
-L/usr/local/android-ndk/platforms/android-9/arch-mips/usr/lib -L/home/eyal/devel/openssl/lib
../lib/.libs/libcurl.so -lz
OK... It looks like you specified the path to libcrypto and libssl:
-L/home/eyal/devel/openssl/lib
But it does not look like you passed in the actual libraries:
-lssl -lcrypto
I'm using --host=mipsel-linux-android which might be wrong...
This looks OK to me. Here's what the folks on the Autotools list told me to use for Android/ARM:
./configure --build=`./config.guess` --host=arm-linux-androideabi
I'd expect MIPS to use something like this because of what the Autotools folks passed on:
./configure --build=`./config.guess` --host=mipsel-linux-android
But I still think yours is OK.

How to link multiple static libs in android shared lib

I have pre build boost libs for android using standalone tool chain 4.8.
I want to use he following boost libs and added as following in Android.mk
LOCAL_STATIC_LIBRARIES += libboost_atomic \
libboost_date_time \
libboost_exception \
libboost_thread \
libboost_system \
libboost_filesystem
I have adde my own static lib liblocal.a in Android.mk.
I have a mainactivity.cpp file in my JNI folder of android project.
I am including header.h in mainactivity.h. header.h had class declaration for classA which uses boost libs.
while building liblocal.a, I am not getting any link error for boost.
While inclduding header.h in mainactivity.h I am getting the following link error:
../../../3p/boost/android/include/boost/system/error_code.hpp:222: error: undefined reference to 'boost::system::generic_category()'
../../../3p/boost/android/include/boost/system/error_code.hpp:223: error: undefined reference to 'boost::system::generic_category()'
../../../3p/boost/android/include/boost/system/error_code.hpp:224: error: undefined reference to 'boost::system::system_category()'
../../../3p/boost/android/include/boost/system/error_code.hpp:222: error: undefined reference to 'boost::system::generic_category()'
../../../3p/boost/android/include/boost/system/error_code.hpp:223: error: undefined reference to 'boost::system::generic_category()'
../../../3p/boost/android/include/boost/system/error_code.hpp:224: error: undefined reference to 'boost::system::system_category()'
../../../3p/boost/android/include/boost/system/error_code.hpp:224: error: undefined reference to 'boost::system::system_category()'
Thanks,
Birajendu
If liblocal.a depends on functions defined in libboost_foo.a, then it must appear before it in the list of static libraries, i.e.:
LOCAL_STATIC_LIBRARIES += liblocal.a libboost_foo.a # GOOD
Should work, while the following:
LOCAL_STATIC_LIBRARIES += libboost_foo.a liblocal.a # BAD
will not, and result in a link error like the one you described.
A work-around is to use LOCAL_WHOLE_STATIC_LIBRARIES that always forces all content from the listed static libraries to be included in the result (this typically generates bloated binaries, but may be necessary if you have circular dependencies).
Hope this helps.
Solved:
I have added LOCAL_LDLIBS += -L$(RELATIVE_PATH_TO_LIB) -lmylib -lmylib1, this solved this issue.
Where as mylib.a and mylib1.a are my pre build static libs.

Android - error: undefined reference to 'vtable for __cxxabiv1::__si_class_type_info'

Ok, I give up...
I've been trying to compile android's 4.x (x>=1) webkit to bypass the 4.0 bugs.
After a few compilations, of the libwebcore with correct bindings and different namespace, and android.webkit with different namespace and some code fixes to match 4.0 - it compiles, and works, on Android 4.3.
Additional problems came in when trying to run it on 4.0-4.2 - where I found out the libicuuc is incompatible between versions, as versioning occurs in the function names. So the solution is theoretically simple - compile libicuuc statically into the libwebcore, by adding LOCAL_STATIC_LIBRARIES += libicuuc to the Android.mk
This is where hell began: I get errors like error: undefined reference to 'vtable for __cxxabiv1::__si_class_type_info' and error: undefined reference to '__dynamic_cast'.
I tried adding LOCAL_RTTI_FLAG := -frtti but it didn't change anything.
Any ideas?
Thanks!

Categories

Resources