Linux平台关于可执行程序的动态库加载路径(rpath)的查看与修改
- chrpath(change run path)
sudo apt-get install chrpath -y
·查询rpath:
chrpath -l path/to/executable
#or
readelf -d executable |grep RPATH
·修改rpath:($ORIGIN是一个特殊的变量,指示实际的可执行文件名。它在运行时解析到可执行文件的位置,在设置RPATH时非常有用)
chrpath -r "\$ORIGIN:\$ORIGIN/lib:\$ORIGIN/../lib" executable
- patchelf
sudo apt-get install patchelf -y
·查询rpath:
# patchelf主要用来修改rpath,因此读取rpath还是得用readelf
readelf -d executable |grep RPATH
·修改rpath:
patchelf --set-rpath "\$ORIGIN:\$ORIGIN/lib:\$ORIGIN/../lib" executable
- 通过环境变量指定可执行程序加载库路径
vim ~/.bashrc
export LD_LIBRARY_PATH=/home/user/libs
source ~/.bashrc