读书人

rpath增添依赖库搜索路径

发布时间: 2012-08-29 08:40:14 作者: rapoo

rpath添加依赖库搜索路径

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:
make LDFLAGS="'-Wl,-R,\$\$ORIGIN'"

其他有待调查的问题1. ld -z -origin 或 g++ -Wl,-z,origin
不知道这个有什么作用,文档里一笔带过没看明白。readelf 看了下多了类似与如下的一行
0x6ffffffb (FLAGS_1) Flags: ORIGIN

2. 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 查看

3. clang
4. RUNPATH, DT_RUNPATH

参考文献:http://itee.uq.edu.au/~daniel/using_origin/
http://stackoverflow.com/questions/9263256/can-you-please-help-me-understand-how-mach-o-libraries-work-in-mac-os-x
http://itee.uq.edu.au/~daniel/using_origin/
http://stackoverflow.com/questions/6324131/rpath-origin-not-having-desired-effect
http://www.delorie.com/gnu/docs/binutils/ld_3.html
http://labs.qt.nokia.com/2011/10/28/rpath-and-runpath/
http://blog.csdn.net/dbzhang800/article/details/6918413


读书人网 >其他相关

热点推荐