Everything About static libraries in C
Static libraries are a crucial component when we are creating our programs. These libraries contains several object files with all the symbols (functions, variables an so on) needed for our program to work. 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. These libraries are later used in the last step of the compilation called linking to be combined with our code to form the final executable of the program that is going to perform our desired task. So, is important to know how they work and how we can create and use these libraries.
What is a static library?
A static library is a collection of object files that are linked into the program during the linking phase of compilation and are not relevant during runtime. We can think this collection of object files as a translation of the c code of all our functions into machine language, for example functions to manipulate strings or for printing to the console. The libraries in C can be of two types, static libraries or dynamic libraries, the main difference between them is that dynamic libraries are just referenced during compilation and are not attached to the executable program. Dynamic libraries are linked during runtime by the dynamic loader to the memory to be used there for our program. The best example of dynamic libraries is the standard C library.
Why to use static libraries?
The main reason to use static libraries is speed. Static libraries are indexed entities, this means all its symbols are in order so this increase the speed when looking up for some function or variable. Also, the library is linked directly to the executable so there is no need for the program to look in different parts of the disk.
How to create a static library
The main tool to create static libraries in C is the “ar” command, we can see the syntax in the following example:
ar rc [lib(Name of the library).a] [Source object files]
in the first brackets we are going to define the name of the library with the syntax described before so if we want to create a library called holberton we should name it libholberton.a, later we are going to list all the object files that we want to include in our library separated by a blank space. The rc options are to insert the member into the archive with replacement and to create always the archive if it did not exist when you request an update.
In order to create our object files, we need to create them using the following command
gcc -c [source C files]
With this command we are going to take all the functions stored in the .c extensions files and turn it them into source files to latter feed them into our ar command. The -c option tells the compiler to stop before the linking process so with this we are going to obtain our .c files as .o source files.
After the creation of our library 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.