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 C library libc
. 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.
Library code can be linked in two different ways:
- Statically: This means that all the library functions your application calls and their dependencies are pulled from the library archive and bound into your executable.
- Dynamically: This means that references to the library files and functions in those files are generated in the code but the actual linking is done dynamically at load time.
You will find the code for the examples that follow in the book code archive in MELD/Chapter02/library
.
Static libraries
Static linking is useful in a few circumstances. For example, if you are...