How to let gcc print ALL libraries linked?
c, c++, gcc
Solution
$ gcc -o hello -Xlinker -v hello.c
collect2 version 4.6.3 (x86-64 Linux/ELF)
/usr/bin/ld --sysroot=/ --build-id --no-add-needed --as-needed --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o hello /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.6 -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../.. -v /tmp/ccvjXRF7.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o
GNU ld (GNU Binutils for Ubuntu) 2.22
`-Xlinker --verbose` will give even more info, including exactly which libraries are resolved and included. An excerpt:
attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.so failed
attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.a succeeded
attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so succeeded
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so)
attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o succeeded
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o
attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o succeeded
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o
ld-linux-x86-64.so.2 needed by /lib/x86_64-linux-gnu/libc.so.6
found ld-linux-x86-64.so.2 at /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
To list just the dynamically linked libaries, run `ldd` on the resulting binary.
$ ldd hello
linux-vdso.so.1 => (0x00007fff68dad000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fac49f46000)
/lib64/ld-linux-x86-64.so.2 (0x00007fac4a323000)
Problem
Even for the simplest "hello world" program, which can be compiled and linked using command ``` "gcc -o hello hello.c" ``` , there must be some version of standard C library linked to build target from hello.o. What I was looking for was exactly that kind of libraries that were "secretly" linked by gcc in a compile-link process. Is there anyway to do that?