目录

rpath添加依赖库搜索路径

rpath添加依赖库搜索路径

from: http://blog.csdn.net/ibingow/article/details/7882098

windows 下一个程序的dll搜索路径首先是程序目录,然后是一些系统目录。linux 下的可以通过设置 LD_LIBRARY_PATH 等方法来添加搜索路径,但是这些路径是定死的,如果要实现程序目录,就要使用链接时的rpath选项。rpath 适用于 elf 文件,不适用 pe。

Linux 上动态库搜索顺序:

Unless loading object has RUNPATH:

RPATH of the loading object,

then the RPATH of its loader (unless it has a RUNPATH), …,

until the end of the chain, which is either the executable

or an object loaded by dlopen(loader 为可执行程序或被 dlopen 打开的对象)

Unless executable has RUNPATH:

RPATH of the executable

LD_LIBRARY_PATH

RUNPATH of the loading object

ld.so.cache

default dirs

rpath 用法

ld:

[plain]

  1. -rpath dir or –rpath=dir

为了兼容其他 ELF 链接器,如果 -R 选项的值是个目录而不是文件,那么此时相当与 -rpath

注:-R filename 与 –just-symbols=filename 等价

如果链接时没使用 ‘-rpath’ ,那么如果定义了 LD_RUN_PATH 就会使用该值作为 rpath

ld 如果不是直接被调用的,而是是通过一个驱动程序如 gcc 调用,那么的所有的链接选项都需要加上前缀 “-Wl,", 选项和值之间用”,“隔开

所以gcc的rpath用法:

[plain]

  1. -Wl,-rpath,dir

多个dir之间用冒号分隔:

[plain]

  1. -Wl,-rpath,dir1:dir2:…:dirN
$ORIGING

代表程序目录,需要加单引号,否则会被解释为变量ORIGIN的值。这样gcc的链接flags为

[plain]

  1. -Wl,-rpath,’$ORIGIN’

[plain]

  1. ‘-Wl,-rpath,$ORIGIN’

用 readelf 查看编译后的程序

[plain]

  1. readelf -d path

会发现多了类似与这么一行

[plain]

  1. 0x0000000f (RPATH)                      Library rpath: [$ORIGIN]

这样,程序运行前会先搜索程序所在路径有没有依赖的库。

Makefile 中的写法:

注意Makefile里要用两个$$,只用一个$会展开成变量值,$$相当与转义成$

[plain]

  1. LFLAGS += ‘-Wl,-rpath,$$dir’
qmake 工程中的写法:

[plain]

  1. QMAKE_LFLAGS += -Wl,-rpath,'$$dir'

[plain]

  1. QMAKE_RPATHDIR += :'$$dir'

注意QMAKE_RPATHDIR最开始要加冒号,因为生成Makefile会自动在前面加上-Wl,-rpath,$$PWD

bash 里执行 make 命令:

[plain]

  1. make LDFLAGS=”’-Wl,-R,$$ORIGIN'"
其他有待调查的问题
  1. ld -z -origin 或 g++ -Wl,-z,origin

不知道这个有什么作用,文档里一笔带过没看明白。readelf 看了下多了类似与如下的一行

0x6ffffffb (FLAGS_1)                    Flags: ORIGIN

  1. macx

@executable_path : relative to the main executable

@loader_path : relative to the referring binary

@rpath : relative to any of a list of paths.

from qtcreator/src/rpath.pri:

!isEmpty(TIGER_COMPAT_MODE) {

QMAKE_LFLAGS_SONAME = -Wl,-install_name,@executable_path/../PlugIns/

} else {

QMAKE_LFLAGS_SONAME = -Wl,-install_name,@rpath/PlugIns/

QMAKE_LFLAGS += -Wl,-rpath,@loader_path,-rpath,@executable_path

}

使用 otool 查看

  1. clang

  2. RUNPATH, DT_RUNPATH

参考文献: