问题描述
我构建了一个使用 app
的可执行文件 liba
。 liba
反过来需要 libb
。 libb
反过来需要 libc
。依赖关系如下所示:app
-> liba
-> libb
-> libc
。 app
和 liba
由我构建,而 libb
和 {{1 }} 是第 3 方。我不知道它们是如何编译的,但我知道它们都在同一个文件夹中,并且我有一个 CMake 配置来查找 libc
。
无法正常工作的设置。
在 libb
中,我找到了 CMake
的包,将 lib 公开链接到 libb
,最后链接 liba
到 liba
。 app
编译正常,但运行它我得到:app
。 IE。最后一个库,我什至没有直接使用的符号都找不到。
我使用error while loading shared libraries: libc: cannot open shared object file: No such file or directory
检查我的图书馆liba
:
readelf -d liba | grep PATH
所以路径在我的 ...
0x000000000000001d (RUNPATH) Library runpath: [/path/to/dir/with/both/libb/and/libc:]
...
中,它是正确的并且指向 liba
和 libb
.
接下来,我使用 libc
检查加载搜索路径:
LD_DEBUG=libs ./app
怎么会在 ...
27012: find library=libb [0]; searching
27012: search path=/*a lot of paths INCLUDING the needed path*/ (RUNPATH from file ./app)
...
27012: find library=libc [0]; searching
27012: search cache=/etc/ld.so.cache
27012: search path=/*a lot of system paths,but the needed path is MISSING*/ (system search path)
27012: trying file=/lib/x86_64/libc
many other tries
...
中找到 libb
而 RUNPATH
却没有???!为什么 libc
使用 libc
而不是 system search path
??
有效的设置
接下来,我尝试通过设置 RUNPATH
标志在构建我的库 RPATH
时使用 liba
。现在 -Wl,--disable-new-dtags
向我打印与以前相同的路径,但缩写为 readelf -d liba | grep PATH
而不是 RPATH
和 RUNPATH
打印:
LD_DEBUG=libs ./app
现在两个库都使用 ...
27012: find library=libb [0]; searching
27012: search path=/*a lot of paths INCLUDING the needed path*/ (RUNPATH from file ./app)
...
27012: find library=libc [0]; searching
27012: search path=/*a lot of paths INCLUDING the needed path*/ (RUNPATH from file ./app)
...
但我不明白为什么。为什么他们使用 RUNPATH
尽管 lib 是用显式 RUNPATH
编译的??这是从哪里来的?
更新:
这里的 RPATH
我指的是我的第 3 方库(a、b、[libc]
),而不是系统库。
c
更新 2:
更多阅读后,我能够更准确地制定搜索请求并立即找到解释。例如,这里:
c++ secondary dependency resolution with runpath
Dynamic linking with rpath not working under Ubuntu 17.10
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)