最近做NDK项目时,遇到了个很让人恼火的问题,调用动态库时,一旦API参数使用了STL容器,就无法链接。报错类似
undefined reference to ‘Func(std::__1::vector&)’
反复检查API文档与自己的程序,并未发现参数不一致的情况。检查库的依赖关系,也一切正常。最后使用
arm-linux-androideabi-nm -D -C foobar.so
-D:动态库,-C:还原经过Name mangling的函数名
导出动态库中的函数名后发现,库的函数名为
Func(std::vector&)
与上文的报错信息相比,少了“__1”,命名空间分别为std和std::__1,由于命名空间的不同导致了编译器找不到函数的实现。
查找资料后发现,从Android 7.1.1 r13的SDK开始,NDK的默认编译器由gcc更换为了clang。clang的stl实现不同于gcc的libstdc++,而是libc++。两者对STL的实现方式不同。为保证二进制兼容性, clang通过内联命名空间为自己的STL实现增加了std::__1的前缀。
In order to turn this run time crash into a link time error, libc++ uses a C++ 11 language feature called inline namespace to change the ABI of std::string without impacting the API of std::string. That is, to you std::string looks the same. But to the linker, std::string is being mangled as if it is in namespace std::__1. Thus the linker knows that std::basic_string and std::__1::basic_string are two different data structures (the former coming from gcc’s libstdc++ and the latter coming from libc++).
解决方案有两个,一是使用clang重新编译该动态库,二是编译该工程时,加入clang编译器选项stdlib=libstdc++,使用与动态库相同的STL实现。
参考文献
https://stackoverflow.com/questions/8454329/why-cant-clang-with-libc-in-c0x-mode-link-this-boostprogram-options-examp
https://stackoverflow.com/questions/31617034/stdfuture-and-clang-with-stdlib-libstdc
https://zhuanlan.zhihu.com/p/27470060

在使用NDK进行项目开发时,遇到动态库调用时涉及STL容器的API链接失败问题。错误源于库函数名缺少'__1'前缀,这是由于从Android 7.1.1开始,NDK默认编译器从gcc变为clang,导致STL实现的变化。clang的libc++使用inline namespace使得std::string在链接时与gcc的libstdc++产生差异。解决方法包括使用clang重新编译动态库或在工程编译时指定相同STL实现的clang选项。
1429

被折叠的 条评论
为什么被折叠?



