搜索和链接库目录的顺序 [英] Order in which library directories are searched and linked
问题描述
我很难理解搜索目录以链接到库的顺序.我有一个CentOS6系统和3个版本的gcc,4.4.7、4.7.2、4.9.2.系统版本为4.4.7,版本4.7.2和4.9.2为模块.在/etc/ld.so.conf.d/
中,有两个文件gcc-4.7.2.conf
和gcc-4.9.2.conf
,它们包含指向4.7.2和4.9.2库的路径.
I am having difficulty understanding the order in which directories are searched for linking to libraries. I have a CentOS6 system and 3 versions of gcc, 4.4.7, 4.7.2, 4.9.2. The system version is 4.4.7 and version 4.7.2 and 4.9.2 are modules. In /etc/ld.so.conf.d/
there two files, gcc-4.7.2.conf
and gcc-4.9.2.conf
which contain the paths to the 4.7.2 and 4.9.2 libraries.
我创建了一个简单的C ++程序main.cpp
I created a simple C++ program, main.cpp
#include <cstdio>
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello You!" << endl;
printf("Back at you!\n");
return 0;
}
使用g ++-4.4.7编译并运行ldd a.out
,我看到
compiling it with g++-4.4.7 and running ldd a.out
, I see
linux-vdso.so.1 => (0x00007fff5535b000)
libstdc++.so.6 => /nonstandardpath/gcc-4.7.2/lib64/libstdc++.so.6 (0x00002ac12de73000)
libm.so.6 => /lib64/libm.so.6 (0x00002ac12e17a000)
libgcc_s.so.1 => /nonstandardpath/gcc-4.7.2/lib64/libgcc_s.so.1 (0x00002ac12e3ff000)
libc.so.6 => /lib64/libc.so.6 (0x00002ac12e614000)
/lib64/ld-linux-x86-64.so.2 (0x00002ac12dc51000)
看着man ld
,它指出(在-rpath-link=dir
下):
Looking at man ld
, it states (under -rpath-link=dir
) :
链接器使用以下搜索路径来查找所需的共享 库:
The linker uses the following search paths to locate required shared libraries:
-
-rpath-link选项指定的任何目录.
Any directories specified by -rpath-link options.
-rpath选项指定的任何目录. -rpath和 -rpath-link是由-rpath选项指定的目录包含在 可执行文件并在运行时使用,而-rpath-link选项仅在以下位置有效 链接时间.仅本地链接器支持以这种方式搜索-rpath,并且 已使用--with-sysroot选项配置的交叉链接器.
Any directories specified by -rpath options. The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time. Searching -rpath in this way is only supported by native linkers and cross linkers which have been configured with the --with-sysroot option.
在ELF系统上,对于本机链接程序,如果-rpath和-rpath-link选项不是 使用时,搜索环境变量"LD_RUN_PATH"的内容.
On an ELF system, for native linkers, if the -rpath and -rpath-link options were not used, search the contents of the environment variable "LD_RUN_PATH".
在SunOS上,如果未使用-rpath选项,则搜索使用以下命令指定的任何目录 -L选项.
On SunOS, if the -rpath option was not used, search any directories specified using -L options.
对于本机链接器,搜索环境变量的内容 "LD_LIBRARY_PATH".
For a native linker, the search the contents of the environment variable "LD_LIBRARY_PATH".
对于本机ELF链接器,共享目录的"DT_RUNPATH"或"DT_RPATH"中的目录 在库中搜索所需的共享库. "DT_RPATH"条目是 如果存在"DT_RUNPATH"条目,则忽略.
For a native ELF linker, the directories in "DT_RUNPATH" or "DT_RPATH" of a shared library are searched for shared libraries needed by it. The "DT_RPATH" entries are ignored if "DT_RUNPATH" entries exist.
默认目录,通常是/lib和/usr/lib.
The default directories, normally /lib and /usr/lib.
对于ELF系统上的本机链接器,如果文件/etc/ld.so.conf存在,则该列表 该文件中找到的目录的数量.
For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that file.
如果未找到所需的共享库,则链接器将发出警告,并且 继续链接.
If the required shared library is not found, the linker will issue a warning and continue with the link.
它没有说明目录的搜索顺序.从上面的示例来看,似乎在/usr/lib
或/lib
It does not state the order in which the directories are searched. From my example above, it appears that /etc/ld.so.conf.d
is searched before /usr/lib
or /lib
问题:链接程序搜索库(例如LD_LIBRARY_PATH,ld.so.conf.d,-rpath和-L)的顺序是什么?
QUESTION : what is the order in which linker searches for libraries (e.g. LD_LIBRARY_PATH, ld.so.conf.d, -rpath, -L)?