交叉编译时,链接器无法找到到存在于 rpath 里的某个动态库的符号

2019-10-23 04:07:47 +08:00
 dangyuluo

标题太拗口,用英语复述一下: Why cross compile linker(collector) is not able to find symbols from a shared libary whose path is already in rpath?

问题是这样的,我们在编译一个程序的时候,报了很多undefined reference to错误。以下是最终编译命令:

/usr/bin/aarch64-linux-gnu-g++ \
-v \
--sysroot=/home/john.doe/gc/sysroot_arm64v8_ubuntu_bionic \
-frecord-gcc-switches \
-O2 -g -DNDEBUG \
CMakeFiles/test_rmw_impl_id_check_exe.dir/rcl/test_rmw_impl_id_check_exe.cpp.o \
-o test_rmw_impl_id_check_exe \
-Wl,-rpath,/path_to_lib_b... \
../librcl.so \
/home/john.doe/gc/liba.so \
-lpthread \
-ldl \

简单来讲,test_rmw_impl_id_check_exe被明文链接到liba.so,而liba.so又需要用libb.so里面的一些符号(可以从以下代码看出)

$ readelf -g /home/john.doe/gc/liba.so | grep NEEDED

0x0000000000000001 (NEEDED)             Shared library: [libb.so]
....

可以很明显的看到libb.so的目录已经很明显地存在于rpath里了,但是 linker 死活都是找不到这些符号。请问各位大神,有什么好办法进行 debug 么?谢谢。

3111 次点击
所在节点    C
3 条回复
dangyuluo
2019-10-23 04:13:08 +08:00
嘿,我发现用`rpath-link`的话就可以。
choury
2019-10-23 13:40:50 +08:00
rpath 是给运行时 ld 找 so 时用的,linker 只是把他写到 elf 里面,编译的时候不会关心这个路径的
dangyuluo
2019-10-24 00:47:21 +08:00
@choury 谢谢你的回复,其实这也是我的理解,直到我读到了这句话:

Searching -rpath in this way is only supported by native linkers and cross linkers which have been configured with the --with-sysroot option.

但是我在交叉编译,所以默认不从 rpath 寻找。不过不知道为什么链接器又不认--with-sysroot 这个参数,真是奇怪。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/611930

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX