Reputation: 1518
When I link executable elf file dynamically it needs libc.so.6 shared library. When I link executable elf file statically it doesn't need libc.so.6 shared library (it's not surprise).
Does it mean, that to assemble executable file with --static, linker includes libc.so.6 in it? If not - what file does linker include? where can I search it? As far as I know, linker includes static libraries in statically assembled file.
Upvotes: 3
Views: 3650
Reputation: 59445
If you link as static, the linker will link all the needed object (.o
) files from the static libraries (.a
). For example, the following command lists the object files which are included in the libc6 library:
ar t /usr/lib/libc.a
(the exact path to libc.a of course differs from system to system)
So answer to your question is no, it will not link the whole libc6 library, but only the needed object files. And also, it doesn't do anything with libc.so.6
, since this is only for dynamic linking. It works with the libc.a
- static version of the library.
According to @janneb comment, the smallest unit to be linked is "section", so it might not even need to link the whole object files.
Upvotes: 3
Reputation: 1
The linker is the ld
command. If you use that command, it does what you ask. Notice that GNU ld
can accept scripts
However, most people are using the gcc
command. This is a compiler from the Gnu Compiler Collection suite. Actually, the gcc
command is just a driver program, which will run cc1
(the proper C compiler), as
, ld
and collect2 (which deals with initializations, etc. then invoke the linker).
To understand what exact command[s] gcc
is running, pass it the -v
program flag.
When you pass -static
to gcc
it will probably link with e.g. /usr/lib/x86_64-linux-gnu/libc.a
or some other static form of the GNU Libc library.
Upvotes: 1