A static library is an archive of object files. Used as a linker input, the linker extracts the object files it needs to carry on the linkage.
The needed object files are those that provide the linker with definitions for symbols that it finds are used, without definition, in other input files. The needed object files, and no others, are extracted from the archive and input to the linkage exactly as if they were individual input files in the linkage command and the static library was not mentioned at all.
Linkers may differ as to whether the position of a static library in the sequence
of input files affects its availability to be searched for needed object files.
Some linkers (e.g. GNU ld
) will search a static library only to obtain definitions
for unresolved symbol references used in earlier input files. For such a linker,
success requires a static library to be input after all other files that depend on it for symbol
definitions. Other linkers (e.g. Microsoft link
) will search a static library to obtain
a definition for any otherwise unresolved symbol reference.
A linker will normally support an option (GNU ld
: --whole-archive
, MS link
: /WHOLEARCHIVE
)
to override the default processing of static libraries and instead link all the contained object files,
whether they are needed or not.
A static library contributes nothing to a linkage except the object files that are extracted from it, which may be vary in different linkages. It is to be contrasted with a shared library, another kind of file altogether with a very different role in linkage.