Reputation: 31
I am a regular C programmer, and there's something I have wondered for some time about GNU GCC/LD internals.
I have noticed that, when passing a sequence of object files to GCC (e.g gcc main.o otherfile.o
), GCC automatically links libc.a
library file without my explicitly specifying -lc
in the options. Similarly, when I compile a program using ncurses
, I just need to specify -lncurses
and libtinfo.a
gets linked automatically (no need to specify -ltinfo
). In other words, even though ncurses
functions use functions from libtinfo
(for instance, unctrl()
), I don't need to explicitly link libtinfo.
How can it be possible?
Does GCC/LD have a list of "default libraries" where it looks for missing symbols when linking? If such a table exists, where is it and how can it be configured?
Upvotes: 2
Views: 1896
Reputation: 753665
A direct answer is that you can see what libraries are linked by command line options by adding -v
to the linking command line. This will show you the commands as they are executed.
The C library and the GCC support library or libraries are linked automatically; other libraries have to be specified manually.
The case of -lncurses
and libtinfo.a
(libtinfo.so
?) is rather different. There, the link command used to build libncurses.so
tells the linker that this library also needs -ltinfo
, so it automatically picks up the extra library.
Upvotes: 2