NOTE:
Because this section tries to cover the widest possible audience,
it may provide more
background than many users will need to link their
programs with a C language library.
If you are interested only in the how-to, and are comfortable
with a purely formal presentation that scants motivation
and background alike, you may want
to skip to
the quick-reference guide
in the last subsection.
Link editing refers to the process in which a symbol
referenced in one module of your program is connected
with its definition in another -- more concretely, the process by which
the symbol printf() in
our sample source file hello.c
is connected with its definition in the standard C library.
Whichever link editing model you choose, static or dynamic,
the link editor will search each module of your program,
including any libraries you have used, for definitions of undefined
external symbols in the other modules.
If it does not find a definition for a symbol,
the link editor will report an error by default,
and fail to create an executable program.
Multiply defined symbols are treated differently, however,
under each approach.
For details, see
``Multiply defined symbols''.
The principal difference between static and dynamic
linking lies in what happens after this search is completed:
Under static linking, external references in your program
are connected with their definitions -- assigned addresses
in memory -- when the executable is created.
If the object code is in an archive library,
copies of the archive library object files that satisfy
still unresolved external references in your program
are incorporated in your executable at link time;
if the object code is in a static shared library,
a special section called .lib
that identifies the library code is created in the object file
and the required shared library code is brought into the address space of the
process only at the time the resulting a.out file is executed.
Under dynamic linking, the contents
of a dynamically linked library are mapped into
the virtual address space of your process at run time.
External references in your program
are connected with their definitions when the program is executed.