1. 通过环境变量修改
# 必须是绝对路径
export LD_LIBRARY_PATH=$(pwd)
2. 编译时指定rpath
# 将rpath指定为当前目录
gcc test.c -L . -lhello -Wl,-rpath,'.'
3. 通过chrpath或patchelf修改
sudo apt install chrpath
# 查看当前rpath
chrpath -l a.out
# 修改 rpath
chrpath -r . a.out
sudo apt install patchelf
# chrpath无法修改ld.so的链接路径,只能通过patchelf修改
patchelf --set-rpath . a.out
patchelf --set-interpreter ./ld-linux-x86-64.so.2 a.out
4. CMake设置rpath
set_target_properties(target PROPERTIES SKIP_BUILD_RPATH FALSE)
set_target_properties(target PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
set_target_properties(target PROPERTIES INSTALL_RPATH "\$ORIGIN")
打包依赖
sudo ldd * | grep -v linux-vdso.so.1 | awk '{print $3}' | xargs -i cp -L {} .
相关文章:
跨平台/架构编译