Static libraries vs dynamic libraries
A library in C is a collection of object files that contains all the symbols (function, variables, etc) needed in our program to be executed. An object file contains machine language (assembler and .o extension) and is the result of the first three steps of the compilation process (Preprocessing, compiler and assembly), I explain the entire compilation process on this article.
Libraries simplify life for programmers because it allows to zip several functions, routines, data structures and so on and use them in different programs saving time and program space.
What’s a static library.
A static library is a collection of object files that are linked into the program during the compilation and are not relevant during runtime, these libraries are include into the executable file so the bigger the library the bigger the file. A static library resides into the executable in form of a binary code so it can only be used by this program.
What’s a dynamic library.
A dynamic library is a collection of object files that are linked into the program during runtime but not in the compilation process, during the compilation the libraries are just referenced in the executable by its memory direction. These libraries are stored in the memory and can be shared for multiple programs.
How to create them.
- Compile the object files of the library
These files are compiled using the following command:
gcc -c [source C files]
for example, lets suppose we have two files to be included called add.c and mult.c, so we compile it as follows:
gcc -c add.c mult.c
as a result we are going to have two files add.o and mult.o that are goint o be used on the next step
2. Create the static library using the ar command as follows:
ar rc [lib(Name of the library).a] [Source object files]
ar rc libholberton.a add.o mult.o
After the creation of our library libholberton.a we must index it and this can be achieved using the “ranlib” command, the sintax of the commands is as follows:
with this steps we have our static library ready to be linked to our executable.
- compile the object files using:
gcc -fPIC -c [source C files]
gcc -fPIC -c add.c mult.c
the fPIC flag start the position independent code that is a requirement for shared libraries.
2. Create the shared library
This will create our source files to create our shared library with the following command:
gcc -shared -o [lib(Name of the library).so] [Source object files]
gcc -shared -o libholberton.so add.o mult.o
the shared flag tells the compiler to create the dynamic library with the .so extension.
3. Set the path for the library
with this we have our dynamic library created named libholberton.so, now the next step is to install our dynamic library so dynamic linking can find it when the program is about to run, the command is as follows:
sudo mv libholberton.so /usr/lib/
or we can set the variable with the following command:
Using a dynamic library
with this steps we have our shared library ready to be linked with our executable with the following command:
gcc -L main.c -lholberton -o name of the executable
Advantages and drawback
- The speed of execution is faster because all the libraries are included in the executable in binary form.
- there is no compatible issues due to all the code is contained on the executable
- The executable have less space
- Updates of the library no need a recompilation of the executable
- One library can be shared for multiple programs
- Linking load time will be reduced if the library is already present in memory
- Every program must contain its own library and bigger libraries makes bigger executables
- Updates on the library requires a recompile of the executable
- Slower execution time
- If the library is removed or the path is not correct our executable will not execute correctly