android ndk编译C++ 的undefined reference to '__cxa_end_cleanup'及 __gxx_personality_v0问题

来源:互联网 发布:linux可以用ghost吗 编辑:程序博客网 时间:2024/05/01 05:46
出现这两个错误主要是由于缺少stl支持导致的。
android 的ndk默认是不带 stl相关属性的
这就导致他编译C++代码的时候有时候会链接失败,报一些函数找不到。

比如这两个:
__cxa_end_cleanup 
和  __gxx_personality_v0  

这些stl相关的库不在$(NDK_ROOT)/platforms/android-9/arch-arm/usr/lib里面,而在$(NDK_ROOT)/sources/cxx-stl 下面你用grep 可以看到 
hl@hl-VirtualBox:/opt/android-ndk-r8c/sources/cxx-stl$ grep -rnH "__cxa_end_cleanup"  ./  
Binary file ./gnu-libstdc++/4.4.3/libs/armeabi-v7a/libsupc++.a matches
Binary file ./gnu-libstdc++/4.4.3/libs/armeabi-v7a/libgnustl_shared.so matches
Binary file ./gnu-libstdc++/4.4.3/libs/armeabi-v7a/libgnustl_static.a matches
Binary file ./gnu-libstdc++/4.4.3/libs/armeabi/libsupc++.a matches
Binary file ./gnu-libstdc++/4.4.3/libs/armeabi/libgnustl_shared.so matches
Binary file ./gnu-libstdc++/4.4.3/libs/armeabi/libgnustl_static.a matches
Binary file ./gnu-libstdc++/4.6/libs/armeabi-v7a/libsupc++.a matches
Binary file ./gnu-libstdc++/4.6/libs/armeabi-v7a/libgnustl_shared.so matches
Binary file ./gnu-libstdc++/4.6/libs/armeabi-v7a/libgnustl_static.a matches
Binary file ./gnu-libstdc++/4.6/libs/armeabi/libsupc++.a matches
Binary file ./gnu-libstdc++/4.6/libs/armeabi/libgnustl_shared.so matches
Binary file ./gnu-libstdc++/4.6/libs/armeabi/libgnustl_static.a matches

如果你用ndk自带的nm命令查看这个 libsupc++.a就会发现它里面是有__cxa_end_cleanup 这个函数定义的
hl@hl-VirtualBox:/opt/android-ndk-r8c/sources/cxx-stl$ arm-linux-androideabi-nm ./gnu-libstdc++/4.6/libs/armeabi-v7a/libsupc++.a | grep __cxa_end_cleanup 
         U __cxa_end_cleanup
00000000 T __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup
         U __cxa_end_cleanup

所以我们在编译C++代码的时候如果遇到这类问题,就在Android.mk里面加上:

STL_PATH=$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a
以及
LOCAL_LDLIBS += -L$(STL_PATH) -lsupc++
即可解决这个问题
再次感谢此文作者,说的很详细,对我启发很大
http://blog.csdn.net/andyhuabing/article/details/8591459