ndk 编译的一些随手记

1. 链接静态库的顺序

在链接静态库时,如果多个静态库之间存在依赖关系,则有依赖关系的静态库之间存在链接顺序问题。这在使用静态库时需要注意,否则会报符号找不到的链接错误。

例如:
lib2.a 依赖于 lib1.a,而最终可执行文件 test 依赖于 lib2.a,则链接选项应为:
-llib2.a -llib1.a,而不能反过来,否则会报 lib1.a 中的某些符号未定义。

2. 编译时遇到  undefined references to static stl 的情况  具体log:

undefined reference to `std::__detail::_List_node_base::_M_unhook()'
/home/sonic/workplace/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/stl_list.h:1532: undefined reference to `std::__detail::_List_node_base::_M_unhook()'
/home/sonic/workplace/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/stl_list.h:1532: undefined reference to `std::__detail::_List_node_base::_M_unhook()'
/home/sonic/workplace/project/android_webdemo/jni/webdemo/../Library/libs/poco/1.5.1/Android/libPocoXML.a(AbstractNode.o): In function `std::list<Poco::XML::DOMObject*, std::allocator<Poco::XML::DOMObject*> >::_M_insert(std::_List_iterator<Poco::XML::DOMObject*>, Poco::XML::DOMObject* const&)':
/home/sonic/workplace/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/stl_list.h:1516: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)'
/home/sonic/workplace/project/android_webdemo/jni/webdemo/../Library/libs/poco/1.5.1/Android/libPocoXML.a(ElementsByTagNameList.o): In function `std::list<Poco::XML::DOMObject*, std::allocator<Poco::XML::DOMObject*> >::_M_insert(std::_List_iterator<Poco::XML::DOMObject*>, Poco::XML::DOMObject* const&)':
/home/sonic/workplace/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/stl_list.h:1516: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)'
/home/sonic/workplace/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/stl_list.h:1516: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)'
/home/sonic/workplace/project/android_webdemo/jni/webdemo//libFS.a(FsString.o): In function `FS::int2string(int)':
FsString.cpp:(.text+0xfc): undefined reference to `std::allocator<char>::allocator()'
FsString.cpp:(.text+0x120): undefined reference to `std::allocator<char>::~allocator()'
FsString.cpp:(.text+0x13c): undefined reference to `std::allocator<char>::~allocator()'
/home/sonic/workplace/project/android_webdemo/jni/webdemo//libFS.a(FsString.o): In function `FS::int2wstring(int)':
FsString.cpp:(.text+0x1a8): undefined reference to `std::allocator<char>::allocator()'
FsString.cpp:(.text+0x1d0): undefined reference to `std::allocator<char>::~allocator()'
FsString.cpp:(.text+0x228): undefined reference to `std::allocator<char>::~allocator()'
/home/sonic/workplace/project/android_webdemo/jni/webdemo//libFS.a(FsString.o): In function `FS::wstring2string(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&)':
FsString.cpp:(.text+0x2f8): undefined reference to `std::allocator<char>::allocator()'
FsString.cpp:(.text+0x318): undefined reference to `std::allocator<char>::~allocator()'
FsString.cpp:(.text+0x348): undefined reference to `std::allocator<char>::~allocator()'
/home/sonic/workplace/project/android_webdemo/jni/webdemo//libFS.a(FsString.o): In function `FS::string2wstring(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
FsString.cpp:(.text+0x3f8): undefined reference to `std::allocator<wchar_t>::allocator()'
FsString.cpp:(.text+0x418): undefined reference to `std::allocator<wchar_t>::~allocator()'
FsString.cpp:(.text+0x448): undefined reference to `std::allocator<wchar_t>::~allocator()'

等等.

用的ndk  gnustl_static 。 google 了一下,在 https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/FslGMERCvl0 发现了大牛的回答.

1/ Don't add -lstdc++

2/ -lsupc++ should appear _after_ -lgnustl_static
 
3/ Put the source file _before_ the libraries it depends on
 
关键是第三步.  把链的库写到最后.
 
然后 感受就和楼主一样了  Thank you, it solved everything! =) .


原文地址:https://www.cnblogs.com/soniclq/p/3028018.html