-rpath-link和-L有什么区别? [英] What's the difference between `-rpath-link` and `-L`?

查看:183
本文介绍了-rpath-link和-L有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gold的人指出:

  -L DIR, --library-path DIR
          Add directory to search path

  --rpath-link DIR
          Add DIR to link time shared library search path

bfd ld的人使它听起来像-rpath-link用于递归包含的sos.

The man for bfd ld makes it sort of sound like -rpath-link is used for recursively included sos.

ld.lld甚至没有将其列为参数.

ld.lld doesn't even list it as an argument.

有人可以为我澄清这种情况吗?

Could somebody clarify this situation for me?

推荐答案

这是GNU ld的演示,演示了-L-rpath-link之间的区别- 并且-rpath-link-rpath之间的差异也很好.

Here is a demo, for GNU ld, of the difference between -L and -rpath-link - and for good measure, the difference between -rpath-link and -rpath.

foo.c

#include <stdio.h>

void foo(void)
{
    puts(__func__);
}

bar.c

#include <stdio.h>

void bar(void)
{
    puts(__func__);
}

foobar.c

extern void foo(void);
extern void bar(void);

void foobar(void)
{
    foo();
    bar();
}

main.c

extern void foobar(void);

int main(void)
{
    foobar();
    return 0;
}

制作两个共享库libfoo.solibbar.so:

$ gcc -c -Wall -fPIC foo.c bar.c
$ gcc -shared -o libfoo.so foo.o
$ gcc -shared -o libbar.so bar.o

制作第三个共享库,libfoobar.so取决于前两个;

Make a third shared library, libfoobar.so that depends on the first two;

$ gcc -c -Wall -fPIC foobar.c
$ gcc -shared -o libfoobar.so foobar.o -lfoo -lbar
/usr/bin/ld: cannot find -lfoo
/usr/bin/ld: cannot find -lbar
collect2: error: ld returned 1 exit status

糟糕.链接器不知道在哪里解析-lfoo-lbar.

Oops. The linker doesn't know where to look to resolve -lfoo or -lbar.

-L选项可解决此问题.

$ gcc -shared -o libfoobar.so foobar.o -L. -lfoo -lbar

-Ldir选项告诉链接器dir是要链接的目录之一 搜索可解析给出的-lname选项的库.搜索 首先按命令行顺序-L目录;然后搜索 配置的默认目录,按其配置顺序.

The -Ldir option tells the linker that dir is one of the directories to search for libraries that resolve the -lname options it is given. It searches the -L directories first, in their commandline order; then it searches its configured default directories, in their configured order.

现在制作一个依赖于libfoobar.so的程序:

Now make a program that depends on libfoobar.so:

$ gcc -c -Wall main.c
$ gcc -o prog main.o -L. -lfoobar
/usr/bin/ld: warning: libfoo.so, needed by ./libfoobar.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libbar.so, needed by ./libfoobar.so, not found (try using -rpath or -rpath-link)
./libfoobar.so: undefined reference to `bar'
./libfoobar.so: undefined reference to `foo'
collect2: error: ld returned 1 exit status

再次糟糕.链接器检测libfoobar.so请求的动态依赖关系 但不能满足他们.让我们拒绝它的建议-try using -rpath or -rpath-link- 有点,看看我们可以用-L-l做什么:

Oops again. The linker detects the dynamic dependencies requested by libfoobar.so but can't satisfy them. Let's resist its advice - try using -rpath or -rpath-link - for a bit and see what we can do with -L and -l:

$ gcc -o prog main.o -L. -lfoobar -lfoo -lbar

到目前为止,一切都很好.但是:

So far so good. But:

$ ./prog
./prog: error while loading shared libraries: libfoobar.so: cannot open shared object file: No such file or directory

在运行时,加载程序找不到libfoobar.so.

at runtime, the loader can't find libfoobar.so.

那链接器的建议呢?使用-rpath-link,我们可以做到:

What about the linker's advice then? With -rpath-link, we can do:

$ gcc -o prog main.o -L. -lfoobar -Wl,-rpath-link=$(pwd)

并且这种关联也成功了.

and that linkage also succeeds.

-rpath-link=dir选项告诉链接器,当遇到输入文件时, 请求动态依赖关系-如libfoobar.so-它应搜索目录dir到 解决它们.所以我们不需要-lfoo -lbar指定那些依赖项,并且不需要 甚至需要知道它们是什么.它们是已经写在 libfoobar.so:-

The -rpath-link=dir option tells the linker that when it encounters an input file that requests dynamic dependencies - like libfoobar.so - it should search directory dir to resolve them. So we don't need to specify those dependencies with -lfoo -lbar and don't even need to know what they are. What they are is information already written in the dynamic section of libfoobar.so:-

$ readelf -d libfoobar.so

Dynamic section at offset 0xdf8 contains 26 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libfoo.so]
 0x0000000000000001 (NEEDED)             Shared library: [libbar.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 ...
 ...

我们只需要知道一个目录,无论它们在哪里都可以找到.

We just need to know a directory where they can be found, whatever they are.

但这是否给我们带来了可运行的prog?

But does that give us a runnable prog?

$ ./prog
./prog: error while loading shared libraries: libfoobar.so: cannot open shared object file: No such file or directory

不.和以前一样.这是因为-rpath-link=dir为链接器提供了信息 装载程序将需要来解决prog的某些动态依赖关系 在运行时-假设它在运行时保持为真-但它不会将该信息写入到 prog的动态部分中. 它仅使链接成功,而无需我们阐明所有递归动态 -l选项与链接的依赖关系.

No. Same as story as before. That's because -rpath-link=dir gives the linker the information that the loader would need to resolve some of the dynamic dependencies of prog at runtime - assuming it remained true at runtime - but it doesn't write that information into the dynamic section of prog. It just lets the linkage succeed, without our needing to spell out all the recursive dynamic dependencies of the linkage with -l options.

在运行时libfoo.solibbar.so-实际上是libfoobar.so- 可能不是它们现在所在的位置-$(pwd)-但装载程序可能能够找到它们 通过其他方式:通过 ldconfig 缓存或设置 LD_LIBRARY_PATH环境变量的值,例如:

At runtime, libfoo.so, libbar.so - and indeed libfoobar.so - might well not be where they are now - $(pwd) - but the loader might be able to locate them by other means: through the ldconfig cache or a setting of the LD_LIBRARY_PATH environment variable, e.g:

$ export LD_LIBRARY_PATH=.; ./prog
foo
bar

rpath=dir为链接程序提供与rpath-link=dir相同的信息 指示链接器将该信息烘焙到 输出文件.让我们尝试一下:

rpath=dir provides the linker with the same information as rpath-link=dir and instructs the linker to bake that information into the dynamic section of the output file. Let's try that:

$ export LD_LIBRARY_PATH=
$ gcc -o prog main.o -L. -lfoobar -Wl,-rpath=$(pwd)
$ ./prog
foo
bar

一切都很好.因为现在,prog包含以下信息:$(pwd)是运行时搜索 所依赖的共享库的路径,如我们所见:

All good. Because now, prog contains the information that $(pwd) is a runtime search path for shared libraries that it depends on, as we can see:

$ readelf -d prog

Dynamic section at offset 0xe08 contains 26 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libfoobar.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [/home/imk/develop/so/scrap]
 ...                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ...

将在LD_LIBRARY_PATH中列出的目录之后(如果已设置),并且在系统默认设置之前-ldconfig -ed目录以及/lib/usr/lib,将尝试该搜索路径.

That search path will be tried after the directories listed in LD_LIBRARY_PATH, if any are set, and before the system defaults - the ldconfig-ed directories, plus /lib and /usr/lib.

这篇关于-rpath-link和-L有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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