Linux【8】-软件管理-1-5-修改linux下elf文件library搜索路径(patchelf)

一、安装

github地址:https://github.com/NixOS/patchelf

cd /data/src
git clone https://github.com/NixOS/patchelf.git
cd patchelf


wget -c https://github.com/NixOS/patchelf/releases/download/0.14.3/patchelf-0.14.3.tar.gz

tar -axf patchelf-0.14.3.tar.gz
cd patchelf-0.14.3

./configure --prefix=/usr/local/patchelf
make
make check
sudo make install

环境变量:

vim /etc/profile

#patchelf
export PATH=/usr/local/patchelf/bin/:$PATH


source /etc/profile

版本:

patchelf --version

二、用法

更多用法: https://github.com/NixOS/patchelf

查看依赖库 (假设自己编译的动态库或可执行程序名字为demo,此处以demo为例)

readelf -d demo  

Dynamic section at offset 0x7e3c30 contains 35 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libtorch.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc10.so]
 0x0000000000000001 (NEEDED)             Shared library: [libIlmImf-2_2.so.22]

修改rpath

patchelf --set-rpath '$ORIGIN/' demo

这样就添加了可执行程序demo所在路径为依赖库搜索路径

使用案例:

chmod +x $@ && patchelf --remove-rpath $@ && patchelf --set-interpreter /..路径../lib/ld-linux-x86-64.so.2 --force-rpath --set-rpath /...路径.../lib $@

三、报错

报错1

g++: 错误:unrecognized command line option ‘-std=c++14’

解决办法:

升级gcc即可

参考资料

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn