linker option to list libraries used

I am working on a Linux platform and using the GNU C++ compiler. I am trying to resolve a link error that says some symbols are undefined. I can go find libraries with the name provided on the command line, and see that they contain the symbols in question ( using the 'nm' utility ).

I know that for the compilation step, I can use the command-line flag '-H' instead of '-c' in order to get a list of header files that were #included into the compilation. Is there a similar option for the linker? I figure if I could see the list of files that the linker used to handle each '-lmylibrary' flag, I can troubleshoot further.


If you get an undefined symbol error it means you forgot to link some library, knowing which libraries you link to will probably not be as useful as you may think, because obviously the symbol is missing from those libraries, however you can use the -print-file-name=library option to find out which path gcc would use to link a library, example:

$ gcc -print-file-name=libc.a
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../libc.a

Also passing --trace to the linker shows a similar output

gcc -Wl,--trace myprog.c -o myprog -L. -lmylib
-lmylib (./libmylib.a)
-lgcc_s (/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/libgcc_s.so)
....

(Note in the above that --trace is an argument to the linker so it goes in -Wl . gcc invoked for linking won't emit anything useful for --trace as an argument to gcc its self).

You could also use ldd after you successfully build the program to find out which dynamically linked libraries were used, its output looks like this:

ldd `which ls`
linux-vdso.so.1 =>  (0x00007fff7ffff000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f2e8ea93000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f2e8e88b000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f2e8e682000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2e8e2ee000)
....
链接地址: http://www.djcxy.com/p/53304.html

上一篇: gcc链接器解析符号

下一篇: 链接器选项列出使用的库