搜索和链接库目录的顺序 [英] Order in which library directories are searched and linked

查看:149
本文介绍了搜索和链接库目录的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解搜索目录以链接到库的顺序.我有一个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.confgcc-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:

  1. -rpath-link选项指定的任何目录.

  1. 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)?

推荐答案

解决共享库依赖关系时,动态链接器首先 检查每个依赖项字符串以查看其是否包含斜杠(此 如果包含斜杠的共享对象路径名是 在链接时指定).

When resolving shared object dependencies, the dynamic linker first inspects each dependency string to see if it contains a slash (this can occur if a shared object pathname containing slashes was specified at link time).

如果找到斜线,则依赖项 字符串被解释为(相对或绝对)路径名,并且 使用该路径名加载共享库.

If a slash is found, then the dependency string is interpreted as a (relative or absolute) pathname, and the shared object is loaded using that pathname.

如果共享库依赖项不包含斜杠,则将按以下顺序搜索它:

If a shared object dependency does not contain a slash, then it is searched for in the following order:

  • 使用二进制文件的DT_RPATH动态节属性中指定的目录(如果存在)而DT_RUNPATH属性不存在.不建议使用DT_RPATH.

  • Using the directories specified in the DT_RPATH dynamic section attribute of the binary if present and DT_RUNPATH attribute does not exist. Use of DT_RPATH is deprecated.

使用环境变量LD_LIBRARY_PATH(除非可执行文件以安全执行模式运行;请参见下文).在这种情况下,它将被忽略.

Using the environment variable LD_LIBRARY_PATH (unless the executable is being run in secure-execution mode; see below). in which case it is ignored.

使用二进制文件的DT_RUNPATH动态节属性中指定的目录(如果存在).仅搜索此类目录以查找DT_NEEDED(直接依赖项)条目所需的那些对象,并且不适用于那些必须具有自己的DT_RUNPATH条目的对象的子代.这与DT_RPATH不同,后者用于搜索依赖关系树中的所有子项.

Using the directories specified in the DT_RUNPATH dynamic section attribute of the binary if present. Such directories are searched only to find those objects required by DT_NEEDED (direct dependencies) entries and do not apply to those objects' children, which must themselves have their own DT_RUNPATH entries. This is unlike DT_RPATH, which is applied to searches for all children in the dependency tree.

来自缓存文件/etc/ld.so.cache,该文件包含以前在增强库路径中找到的候选共享对象的已编译列表.但是,如果二进制文件是通过-z nodeflib链接器选项链接的,则默认路径中的共享库将被跳过.硬件功能目录中安装的共享库(请参见下文)比其他共享库优先.

From the cache file /etc/ld.so.cache, which contains a compiled list of candidate shared objects previously found in the augmented library path. If, however, the binary was linked with the -z nodeflib linker option, shared objects in the default paths are skipped. Shared objects installed in hardware capability directories (see below) are preferred to other shared objects.

在默认路径中/lib,然后是/usr/lib. (在某些64位体系结构上,用于64位共享库的默认路径是/lib64,然后是/usr/lib64.)如果二进制文件是使用-z nodeflib链接器选项链接的,则跳过此步骤.

In the default path /lib, and then /usr/lib. (On some 64-bit architectures, the default paths for 64-bit shared objects are /lib64, and then /usr/lib64.) If the binary was linked with the -z nodeflib linker option, this step is skipped.

摘自2017年9月15日的版本.我认为ld的旧版本与此类似.

Taken from the 2017-09-15 version. I assume older versions of ld are similar.

这篇关于搜索和链接库目录的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆