How to Print the Ld(Linker) Search Path

How do I know the path of libraries used by ld?

If the linker is GNU ld, pass the linker option --trace, e.g.

clang -L/usr/local/lib -o app app.o -lfoo -Wl,--trace

If the linker is Darwin mach-o ld, pass the linker option -t, e.g.

clang -L/usr/local/lib -o app app.o -lfoo -Wl,-t

The linker will then report the path of each object file, archive(member) or
dynamic library that it loads.

GCC how to add before the default linker search path by default? LIBRARY_PATH not working

As the GCC manual says, LIBRARY_PATH is the correct environment variable to add directories to the library search path.

If you add -v to the g++ command you should see the LIBRARY_PATH that it uses, and you should see it includes the directory you have specified, and that it gets added to the collect2 command as -L, but you will see it gets added after the standard directories such as -L/usr/lib etc.

I don't know any way to make the directories in LIBRARY_PATH come first, I think you have to use -L for that.

Is it possible to permanently add directories to the default ld search path?

No, it is not possible. The paths that ld uses during the linking phase are configured via scripts which are pulled into ld during its build process. There isn't a way to update these after ld is built. There are other questions and answers related to accomplishing this for GCC specifically which seem to involve the LIBRARY_PATH environment variable. That solution, however, is gcc specific.

modify ld library search path on cluster (no root access)

thank you very much for your suggestions. I found the issue and solved the problem. In essence, I needed these additional flags to configure:

sh ../gcc-6.5.0/configure --prefix=/home/xyz/ --build=x86_64-linux-gnu --enable-languages=c,c++,fortran --with-mpfr=/home/xyz/ --with-gmp=/home/xyz/ --with-mpc=/home/xyz/

These posts were helpful: 1, 2, 3

Thank you again for the time you have invested to help me.

ld library search path for non-standard library name

In version 2.18 of GNU Binutils a feature was added to ld that allows a library to be specified by its exact name.

From the current ld man page:


-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to link.
This option may be used any number of times. If namespec is of the form :filename,
ld will search the library path for a file called filename
, otherwise it will
search the library path for a file called libnamespec.a.

For the original question, the :filename form of namespec is used like this:

g++ -o _myother.so myother.o -L../myname/ -l:_myname.so -shared
  • The option, -L../myname/, adds the path, ../myname/, to the library search path. This path should contain the location of _myname.so at link time.
  • The -l:_myname.so option tells ld to search for a library with the exact name, _myname.so. The lib prefix and .so suffix are not added to _myname.so when searching.

At run time, the shared library _myname.so is searched for in the standard locations.



Related Topics



Leave a reply



Submit