Linking with libraries – static and dynamic linking
Any application you write for Linux, whether it be in C or C++, will be linked with the libc
C library. This is so fundamental that you don't even have to tell gcc
or g++
to do it because it always links libc
. Other libraries that you may want to link with have to be explicitly named through the -l
option.
The library code can be linked in two different ways: statically, meaning that all the library functions your application calls and their dependencies are pulled from the library archive and bound into your executable; and dynamically, meaning that references to the library files and functions in those files are generated in the code but the actual linking is done dynamically at runtime. You will find the code for the examples that follow in the book code archive in MELP/Chapter02/library
.
We'll start with static linking.
Static libraries
Static linking is useful in a few circumstances. For example,...