Linux 如何打印 ld(linker) 搜索路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9922949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print the ld(linker) search path
提问by Talespin_Kit
What is the way to print the search paths that in looked by ldin the order it searches.
打印ld按照搜索顺序查找的搜索路径的方法是什么。
回答by Matthew Slattery
I'm not sure that there is any option for simply printing the full effective search path.
我不确定是否有任何选项可以简单地打印完整的有效搜索路径。
But: the search path consists of directories specified by -L
options on the command line, followed by directories added to the search path by SEARCH_DIR("...")
directives in the linker script(s). So you can work it out if you can see both of those, which you can do as follows:
但是:搜索路径由-L
命令行上的选项指定的目录组成,后跟由SEARCH_DIR("...")
链接描述文件中的指令添加到搜索路径的目录。所以如果你能看到这两个,你就可以计算出来,你可以这样做:
If you're invoking ld
directly:
如果您ld
直接调用:
- The
-L
options are whatever you've said they are. - To see the linker script, add the
--verbose
option. Look for theSEARCH_DIR("...")
directives, usually near the top of the output. (Note that these are not necessarily the same for every invocation ofld
-- the linker has a number of different built-in default linker scripts, and chooses between them based on various other linker options.)
- 该
-L
选项是无论你说他们是。 - 要查看链接描述文件,请添加
--verbose
选项。寻找SEARCH_DIR("...")
指令,通常在输出的顶部附近。(请注意,这些对于每次调用都不一定相同ld
——链接器具有许多不同的内置默认链接器脚本,并根据各种其他链接器选项在它们之间进行选择。)
If you're linking via gcc
:
如果您通过gcc
以下方式链接:
- You can pass the
-v
option togcc
so that it shows you how it invokes the linker. In fact, it normally does not invokeld
directly, but indirectly via a tool calledcollect2
(which lives in one of its internal directories), which in turn invokesld
. That will show you what-L
options are being used. - You can add
-Wl,--verbose
to thegcc
options to make it pass--verbose
through to the linker, to see the linker script as described above.
- 您可以将
-v
选项传递给,gcc
以便它显示它如何调用链接器。事实上,它通常不会ld
直接调用,而是通过一个名为collect2
(位于其内部目录之一)的工具间接调用,该工具又调用ld
. 这将显示-L
正在使用哪些选项。 - 您可以添加
-Wl,--verbose
到gcc
选项使之通过--verbose
通过对连接器,看到上面所描述的链接脚本。
回答by telotortium
On Linux, you can use ldconfig
, which maintains the ld.so configuration and cache, to print out the directories search by ld.so
with
在Linux上,你可以使用ldconfig
,其保持ld.so配置和高速缓存,打印出的目录搜索通过ld.so
与
ldconfig -v 2>/dev/null | grep -v ^$'\t'
ldconfig -v
prints out the directories search by the linker (without a leading tab) and the shared libraries found in those directories (with a leading tab); the grep
gets the directories. On my machine, this line prints out
ldconfig -v
打印出链接器搜索的目录(不带前导选项卡)和在这些目录中找到的共享库(带前导选项卡);将grep
得到的目录。在我的机器上,这行打印出来
/usr/lib64/atlas:
/usr/lib/llvm:
/usr/lib64/llvm:
/usr/lib64/mysql:
/usr/lib64/nvidia:
/usr/lib64/tracker-0.12:
/usr/lib/wine:
/usr/lib64/wine:
/usr/lib64/xulrunner-2:
/lib:
/lib64:
/usr/lib:
/usr/lib64:
/usr/lib64/nvidia/tls: (hwcap: 0x8000000000000000)
/lib/i686: (hwcap: 0x0008000000000000)
/lib64/tls: (hwcap: 0x8000000000000000)
/usr/lib/sse2: (hwcap: 0x0000000004000000)
/usr/lib64/tls: (hwcap: 0x8000000000000000)
/usr/lib64/sse2: (hwcap: 0x0000000004000000)
The first paths, without hwcap
in the line, are either built-in or read from /etc/ld.so.conf.
The linker can then search additional directories under the basic library search path, with names like sse2
corresponding to additional CPU capabilities.
These paths, with hwcap
in the line, can contain additional libraries tailored for these CPU capabilities.
第一个路径,没有hwcap
在行中,要么是内置的,要么是从 /etc/ld.so.conf 中读取的。然后链接器可以在基本库搜索路径下搜索其他目录,名称类似于sse2
附加 CPU 功能。这些路径hwcap
可以包含为这些 CPU 功能量身定制的附加库。
One final note: using -p
instead of -v
above searches the ld.so
cache instead.
最后一个注意事项:使用-p
代替-v
上面搜索ld.so
缓存。
回答by faken
You can do this by executing the following command:
您可以通过执行以下命令来做到这一点:
ld --verbose | grep SEARCH_DIR | tr -s ' ;' \012
gccpasses a few extra -L paths to the linker, which you can list with the following command:
gcc将一些额外的 -L 路径传递给链接器,您可以使用以下命令列出这些路径:
gcc -print-search-dirs | sed '/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,; ,g' | tr \; \012
The answers suggesting to use ld.so.conf and ldconfig are not correct because they refer to the paths searched by the runtime dynamic linker (i.e. whenever a program is executed), which is not the same as the path searched by ld(i.e. whenever a program is linked).
建议使用 ld.so.conf 和 ldconfig 的答案不正确,因为它们指的是运行时动态链接器搜索的路径(即每当执行程序时),这与ld搜索的路径不同(即每当程序已链接)。
回答by armando.sano
The question is tagged Linux, but maybe this works as well under Linux?
这个问题被标记为 Linux,但也许这在 Linux 下也能正常工作?
gcc -Xlinker -v
Under Mac OS X, this prints:
在 Mac OS X 下,这会打印:
@(#)PROGRAM:ld PROJECT:ld64-224.1
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 armv6m armv7m armv7em
Library search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
Framework search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/
[...]
The -Xlinker
option of gcc
above just passes -v
to ld
. However:
上面的-Xlinker
选项gcc
只是传递-v
给ld
. 然而:
ld -v
doesn't print the search path.
不打印搜索路径。
回答by Rapha?l Londeix
The most compatible command I've found for gcc and clang on Linux (thanks to armando.sano):
我在 Linux 上为 gcc 和 clang 找到的最兼容的命令(感谢 armando.sano):
$ gcc -m64 -Xlinker --verbose 2>/dev/null | grep SEARCH | sed 's/SEARCH_DIR("=\?\([^"]\+\)"); */\n/g' | grep -vE '^$'
if you give -m32
, it will output the correct library directories.
如果你给-m32
,它会输出正确的库目录。
Examples on my machine:
我机器上的例子:
for g++ -m64
:
对于g++ -m64
:
/usr/x86_64-linux-gnu/lib64
/usr/i686-linux-gnu/lib64
/usr/local/lib/x86_64-linux-gnu
/usr/local/lib64
/lib/x86_64-linux-gnu
/lib64
/usr/lib/x86_64-linux-gnu
/usr/lib64
/usr/local/lib
/lib
/usr/lib
for g++ -m32
:
对于g++ -m32
:
/usr/i686-linux-gnu/lib32
/usr/local/lib32
/lib32
/usr/lib32
/usr/local/lib/i386-linux-gnu
/usr/local/lib
/lib/i386-linux-gnu
/lib
/usr/lib/i386-linux-gnu
/usr/lib
回答by yfeleke
Mac version: $ ld -v 2, don't know how to get detailed paths. output
Mac版:$ ld -v 2,不知道如何获取详细路径。输出
Library search paths:
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/