How to print the ld(linker) search path
Solution 1
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).
Solution 2
On Linux, you can use ldconfig
, which maintains the ld.so configuration and cache, to print out the directories search by ld.so
with
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.
Solution 3
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:
If you're invoking ld
directly:
- 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.)
If you're linking via 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.
Solution 4
The most compatible command I've found for gcc and clang on Linux (thanks to armando.sano):
$ gcc -m64 -Xlinker --verbose 2>/dev/null | grep SEARCH | sed 's/SEARCH_DIR("=\?\([^"]\+\)"); */\1\n/g' | grep -vE '^$'
if you give -m32
, it will output the correct library directories.
Examples on my machine:
for 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
:
/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
Solution 5
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/
[...]
The -Xlinker
option of gcc
above just passes -v
to ld
. However:
ld -v
doesn't print the search path.
Related videos on Youtube

Talespin_Kit
Updated on July 17, 2022Comments
-
Talespin_Kit 4 months
What is the way to print the search paths that in looked by ld in the order it searches.
-
fons over 9 yearsHe is asking about the linker (ld) and not the loader (ld.so)!
-
Ari about 9 yearsThe --verbose option for the linker did the trick. Very helpful!
-
Tomas almost 9 yearsHow is it possible that if I set
export LD_LIBRARY_PATH=/some/other/dir
, it will not affect output of this command?! Seems it doesn't work 100%? -
thomasa88 over 8 yearsI was trying hard to figure out where the linker was looking and did not find SEARCH_DIR in the output. Turns out as I was using
-T script
my script completely replaced ld's default script and only looked where I pointed. -
kenn about 8 yearsYou hit the spot. I have a linking problem, during linking process linker finds manually installed libraries in
/usr/local/..
which causes missing library error, and linking fails. I have to rename/usr/local
everytime to exclude that search path. Is there a simple way to exclude or override/usr/local
path? -
faken about 8 yearsYou can try to manually specify library paths with the -L option to GCC, which I think (not sure) will override the system library paths. You could also try to set the LIBRARY_PATH env variable before compiling: $ LIBRARY_PATH=/somedir/ gcc ...
-
kenn about 8 yearsI know that linking in command line compiling. I meant a global way to override
ld
s search path. For example sometimes I have to compile a source code frommakefile
or generating makefile fromconfigure
script or fromCMakeLists.txt
or even more complicated ones such asvala
orsrt
. It's hard for me to modifyld
search path in such cases -
faken about 8 yearsWhen using CMake you can select the exact libraries which are used during the configuration phase (some of these entries are shown only in advanced mode). As for configure scripts from Autotools, see this answer: stackoverflow.com/questions/7561509/…. This doesn't answer your question directly, but may help you do what you want.
-
Bruce K over 7 yearsThank you! teeny enhancement -- get rid of a grep or two: sed -n 's/SEARCH_DIR("=\?([^"]\+)"); */\1\n/gp'
-
Daniel Santos about 7 years@fons Funnything is that I got here looking for this answer. :) link-time or run-time path? I guess that's the question. LIBRAY_PATH (link time) vs LD_LIBRARY_PATH.
-
John O'M. over 6 yearsI've found on some platforms (e.g. arm with Linaro toolchain) that ldconfig doesn't actually search the same directories as the run time linker. You can get it to output its search path, and include the paths from
LD_LIBRARY_PATH
by enabling debugging. E.g.LD_DEBUG=libs /lib/ld-linux.so --list cat
(you can use any executable, I pickedcat
as the first thing I could think of). Might be worth grepping for "search path
". Note that if you have an/etc/ld.so.cache
that matches all needed libs, you won't get to see the built-in system search path, because it won't get that far. -
nn0p about 6 yearsIs
gcc
search path the same with these? -
rustyx over 5 yearsBe careful with
ldconfig -v
on FreeBSD, it will permanently remove all configured directories. Useldconfig -r
instead on FreeBSD. -
Jacklynn over 5 yearsI get "cannot open 2: no such file or directory". Running
ld -v 2
-
jww about 5 yearsThe question is tagged Linux, not OS X. I don't believe OS X uses GNU's
ld
. The Binutil folks disabled it in the build scripts. It has been disabled for years. -
pevik almost 5 yearsOn Linux it prints directories as well, but in form of
-Lpath
. So @Raphaël Londeix answer is better. -
bmacnaughton over 4 yearsWhy does this require such an obscure method?
-
pari over 4 yearsThis worked like a charm ! how do we add the directories on this list , linker search path ?
-
coladict almost 2 yearsAh, now this explains why building with local libraries is so broken in Centos. Includes path searches are
/usr/local/include
then/usr/include
, while linker search is/usr/lib64
then/usr/local/lib64