5

解决 C++ 链接到一个绝对路径的动态链接库

 3 years ago
source link: https://zhiqiang.org/coding/cpp-link-with-absolute-path.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

解决 C++ 链接到一个绝对路径的动态链接库

作者: 张志强

, 发表于 2021-01-25

, 共 632 字 , 共阅读 81 次

最近升级系统,出现好多莫名其妙的问题。其中一个便是 G++编译后,发现其中一个动态链接库引用了绝对地址。正常情况下运行ldd bin/auto应该是下面的结果:

libqtutil.so => /home/zhangzq/lib/libqtutil.so (0x00007f8603d17000)
libboost_system.so.1.75.0 => /opt/lib/libboost_system.so.1.75.0 (0x00007f8603d12000)

但实际得到下面的结果:

/home/zhangzq/lib/libqtutil.so (0x00007f8603d17000)
libboost_system.so.1.75.0 => /opt/lib/libboost_system.so.1.75.0 (0x00007f8603d12000)

如果readelf -d bin/auto可以看到/home/zhangzq/lib/libqtutil.so整个绝对路径都被编码到了依赖项。而正常应该是libqtutil.so。这会带来问题,其它人无法访问我个人目录下的文件,导致无法运行程序。

最后用的解决方法是为libqtutil.so添加SONAME,在编译时添加选项:

g++ -Wl,-soname,libqtutil.so ...
  • 为什么这样,不明。
  • 为什么能解决,不明。

留待后续研究。

Q. E. D.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK