What is a static library:
static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.
How a Static library Works:
when you compile a source file you get an object file. Depending on your platform its extension may be .o or .obj or anything else. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed. The linker, when trying to generate an executable tries to resolve the referenced symbols, i.e. locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this. Exact implementation depends on the specific linker and library file format but the basic architecture is as mentioned.
How to create a Static Library:
To create a static library using GCC we need to compile our library code into an object file so we tell GCC to do this using -c
$ gcc -c *.c
Here in the above command , all the .c extension files( C files) in the current working directory have been converted in to their respective object files. Once we have object file(s), we use the GNU ar command to create our final library/archive
$ ar -rc libholberton.a *.o
This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r) .
Whenever files are added to a library, including the initial creation of the library , the library needs to be indexed, which is done with the command
ranlibmakes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references.
$ ranlib libholberton.a
We can also see the symbols in our library, using the command
nm, which lists each symbol’s symbol value, symbol type, and symbol name from object files.
How to use a Static Library:
We have now created a static library libholberton.a and now let us use the static library by invoking it as part of the compilation and linking process when creating a program executable. Incase of gcc we use following flags to create static library
- -l<libraryname without lib prefix and extension>
- -L : specifies the path to the library .We can use -L. inorder to point to the current directory and -L/home/tmp to point to the /home/tmp directory.
gcc main.c -L. -lholberton -o main
Now run the executable program ‘main’
The two main disadvantages are..
- If the library code is updated (say, to fix a bug) you have to recompile your program into a new executable.
- Every program in the system that uses that library contains a copy in its executable. This is very inefficient (and a pain if you find a bug and have to recompile, as per point one).
It is in these cases that we find Dynamic or Shared libraries better. Shared libraries are an elegant way around the problems posed by a static library. A shared library is a library that is loaded dynamically at runtime for each application that requires it. But more on this in another post.