2.4.7. Static and dynamic libraries with GCC
Combining static and dynamic linking balances portability and efficiency. The GNU Compiler Collection (GCC) automatically selects shared objects over static archives unless you configure it otherwise. That resolution order determines which library versions to link and which linker options apply.
Static libraries are packaged as libname.a archive files. Dynamic libraries are packaged as libname.so shared objects. Together, those are the two shapes gcc must choose between when both linking modes are available for the same library name.
For each -lfoo option, gcc searches the library directories (including paths from -Lpath) for libfoo.so first and libfoo.a second. Depending on what gcc finds on the search path, linking proceeds in one of these ways:
- Only the shared object is found, and gcc links against it dynamically.
- Only the archive is found, and gcc links against it statically.
- Both the shared object and archive are found, and by default, gcc selects dynamic linking against the shared object.
- Neither shared object nor archive is found, and linking fails.
Because of these rules, the best way to select the static or dynamic version of a library for linking is having only that version found by gcc. This can be controlled to some extent by using or leaving out directories containing the library versions, when specifying the -Lpath options.
Additionally, because dynamic linking is the default, the only situation where linking must be explicitly specified is when a library with both versions present should be linked statically. There are two possible resolutions: specifying the static libraries by file path instead of the -l option, or using the -Wl option to pass options to the linker.
- Specifying the static libraries by file
Usually, gcc is instructed to link against the foo library with the
-lfoooption. However, it is possible to specify the full path to filelibfoo.acontaining the library instead:$ gcc ... path/to/libfoo.a ...From the file extension
.a, gcc will understand that this is a library to link with the program. However, specifying the full path to the library file is a less flexible method.- Using the
-Wloption The gcc option
-Wlis a special option for passing options to the underlying linker. Syntax of this option differs from the other gcc options. The-Wloption is followed by a comma-separated list of linker options, while other gcc options require space-separated list of options.The ld linker used by gcc offers the
-Bstaticoption to link libraries following this option statically, and-Bdynamicto link them dynamically. After passing-Bstaticand a library to the linker, the default dynamic linking behaviour must be restored manually for the following libraries to be linked dynamically with the-Bdynamicoption.Link a program with library first statically (
libfirst.a) and second dynamically (libsecond.so):$ gcc ... -Wl,-Bstatic -lfirst -Wl,-Bdynamic -lsecond ...注意gcc can be configured to use linkers other than the default ld.