Diaz
Diaz

Reputation: 987

How to let gcc print ALL libraries linked?

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?

Upvotes: 8

Views: 4543

Answers (3)

mfro
mfro

Reputation: 3335

-Wl,-Map,mapfile.txt will let ld produce a load map file containing all libraries linked as well as a cross reference list of symbols

Upvotes: 4

OrangeDog
OrangeDog

Reputation: 38826

$ 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)

Upvotes: 15

Jonathan Wakely
Jonathan Wakely

Reputation: 171433

You can either pass -v to GCC which tells it to print out all the options it uses for compilation and how the linker is invoked, or you can tell GCC to pass -v just to the linker, with -Wl,-v and that will print just the linker command, including all the libraries being linked to.

Upvotes: 1

Related Questions