windows 下一个程序的dll搜索路径首先是程序目录,然后是一些系统目录。linux 下的可以通过设置 LD_LIBRARY_PATH 等方法来添加搜索路径,但是这些路径是定死的,如果要实现程序目录,就要使用链接时的rpath选项。rpath 适用于 elf 文件,不适用 pe。
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
注:-R filename 与 --just-symbols
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:-rpath dir or --rpath=dir
为了兼容其他 ELF 链接器,如果 -R 选项的值是个目录而不是文件,那么此时相当与 -rpath
注:-R filename 与 --just-symbols