linux gnu - How to print the ld(linker) search path
On Linux, you can use
ldconfig, which maintains the ld.so configuration and cache, to print out the directories search by
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
/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.
One final note: using
-p instead of
-v above searches the
ld.so cache instead.
What is the way to print the search paths that in looked by ld in the order it searches.
You can do this by executing the following command:
ld --verbose | grep SEARCH_DIR | tr -s ' ;' \\012
gcc passes a few extra -L paths to the linker, which you can list with the following command:
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).
The question is tagged Linux, but maybe this works as well under Linux?
gcc -Xlinker -v
Under Mac OS X, this prints:
@(#)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/ [...]
-Xlinker option of
gcc above just passes
doesn't print the search path.