Search code examples
linkerclangllvmldllvm-clang

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


Say I'm building and linking my application with clang

clang -I/usr/local/include -c -o app.o main.c
clang -L/usr/local/lib -o app app.o -lfoo

How do I know where libfoo.a or libfoo.dylib is located? Is there a verbose mode?

It's possible to search /usr/lib and /usr/local/lib manually, but doing so would be too tedious when you use many libraries.


Solution

  • 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.