继续上一篇《 linux下so动态库一些鲜为人知的秘密(中) 》介绍so搜索路径,还有一个相似于-path,叫LD_RUN_PATH环境变量, 它也是把路径编译进可执行文件内,不一样的是它只设置RPATH。html
[stevenrao]
$
g++ -o demo -L /tmp/ -ltmp main.cpp
[stevenrao]
$
readelf -d demo
Dynamic section at offset 0xb98 contains 25 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libtmp.so]
....
0x000000000000000f
(RPATH) Library rpath: [/tmp/]
另外还能够经过配置
/etc/ld.so.conf,在其中加入一行
/tmp/
这个配置项也是只对运行期有效,而且是全局用户都生效,须要root权限修改,修改完后须要使用命令
ldconfig 将 /etc/ld.so.conf 加载到
ld.so.cache中,避免重启系统就能够当即生效。
除了前面介绍的那些搜索路径外,还有缺省搜索路径/usr/lib/ /lib/ 目录,能够经过-z nodefaultlib编译选项禁止搜索缺省路径。
[stevenrao] $
g++ -o demo -z nodefaultlib -L/tmp -ltmp main.cpp
[stevenrao] $
./demo
./demo: error while loading shared libraries:
libstdc++.so.6: cannot open shared object file
这么多搜索路径,他们有个前后顺序以下
一、RUMPATH 优先级最高
二、RPATH 其次
三、LD_LIBRARY_PATH
四、/etc/ld.so.cache
五、/usr/lib/ /lib/
查看一个程序搜索其各个动态库另外一个简单的办法是使用 LD_DEBUG这个环境变量;
[stevenrao] $
export LD_DEBUG=libs
[stevenrao] $
./demo
下一篇介绍动态库内符号问题