什么是链接到你不进行函数调用到库的影响? [英] What are the effects of linking to a library you do not make function calls to?
问题描述
我有链接与下面的行-ldl到libdl.so程序的make文件。有没有调用dlopen的或任何的相关功能。什么是用这种方式,即使你不使用任何的功能链接到该库的影响?
I have a make file for a program that links to libdl.so with the following line -ldl. There are no calls to dlopen or any of the related functions. What is the effect of linking to this library in this way even though you do not use any of the functions?
推荐答案
您必须阅读你的链接的文档。从信息LD
我的Linux / ELF / GNU Binutils的系统(强调)上:
You'll have to read the documentation for your linker. From info ld
on my Linux/ELF/GNU Binutils system (emphasis added):
`--as-needed'
`--no-as-needed'
This option affects ELF DT_NEEDED tags for dynamic libraries
mentioned on the command line after the `--as-needed' option.
Normally the linker will add a DT_NEEDED tag for each dynamic
library mentioned on the command line, regardless of whether the
library is actually needed or not. `--as-needed' causes a
DT_NEEDED tag to only be emitted for a library that satisfies an
undefined symbol reference from a regular object file or, if the
library is not found in the DT_NEEDED lists of other libraries
linked up to that point, an undefined symbol reference from
another dynamic library. `--no-as-needed' restores the default
behaviour.
您可以通过运行 LDD
上一个测试程序,检查自己。在一个简单的测试程序,我得到:
You can check yourself by running ldd
on a test program. On a simple test program, I get:
linux-vdso.so.1 => (0x00007fffd8305000)
libc.so.6 => /lib/libc.so.6 (0x00007f646c669000)
/lib/ld-linux-x86-64.so.2 (0x00007f646c9ca000)
不过,如果我用 -ldl
链接,我得到这样的:
linux-vdso.so.1 => (0x00007fff644f1000)
libdl.so.2 => /lib/libdl.so.2 (0x00007fb9b1375000)
libc.so.6 => /lib/libc.so.6 (0x00007fb9b1014000)
/lib/ld-linux-x86-64.so.2 (0x00007fb9b1579000)
虽然 libdl
不使用我的程序。但是,如果我运行 -Wl GCC, - 按需
, libdl
将不会在链接据。我的测试中,这仅适用于如果轮候册, - 按需
列在命令行上的之前 - LDL
。
Even though libdl
isn't used by my program. However, if I run GCC with -Wl,--as-needed
, libdl
will not be linked in. According to my tests, this only works if -Wl,--as-needed
is listed on the command line before -ldl
.
哪些影响?这意味着你的二进制文件将不无共享库系统上运行,即使你不使用它。这也意味着,如果升级共享库并卸载旧的二进制文件将打破。这不是一个大问题,因为二进制兼容性是熊反正,但我看不出有什么理由不打开轮候册, - 按需
对于一般项目
What are the effects? It means that your binary won't run on systems without the shared library, even though you don't use it. It also means that your binary will break if you upgrade the shared library and uninstall the old one. It's not a big deal because binary compatibility is a bear anyway, but I see no reason not to turn on -Wl,--as-needed
for projects in general.
这篇关于什么是链接到你不进行函数调用到库的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!