Static and Dynamic Libraries Flashcards
what is a library
library is a collection of reusable code that can be linked to other programs
c library
A C library is a collection of compiled modules, a group of object .o files collected together in a single library file.
All of the non-static global identifiers (variables/functions) in each
module are visible to any other program to which the library is linked. Each compiled object file in the library is compiled from a .c source code module.
static libraries
When you link a program to a static library, a copy of the library’s code is added to the resulting executable program
The executable program is permanently associated with the specific version of the static library that you linked it to
to change the version of the library that the program uses is to recompile the entire program and re-link it to the new version of the library.
dynamic libraries
also called a shared library
one copy of the compiled library on disk can be shared by any number of different running programs.
the dynamic library’s compiled code is not copied into the executable program.
The program contains only a “reference” to the library
It is only loaded when some function or variable in the library is actually used at run-time.
This is advantageous because a computer need have only one copy of a shared library stored on its hard drive
dynamic continued
The advantage of dynamic libraries is that a program linked to one may dynamically load a different version of the library than the one it was initially linked to. This allows library code to be upgraded, debugged, and improved,
A dynamic library usually has a .so extension (short for shared object) on LINUX
Linking to a Library
Building a Static Library
ar -r -c -s lib < libname >.a <file1>.o, <file2>.o ... <filen .o></file2></file1>