Static Libraries in language C

One of the tools that compilers supply us with are libraries. Among its advantages is being able to optimize the search in memory, avoid repeating code several times unnecessarily and greater response times in the search.
There are two types of libraries, static and dynamic, but in this blog we will talk about state.
What is a Static Library?
A static library are collections of files and objects that are linked to the program, are used only during the link phase, and are not required during runtime. Normally the library is indexed making it easy to find symbols (functions, variables, etc.) for this reason it is easier to link the object files of a library than independently or separately, which speeds up the linking.

How are they created?
To create a static library we will use the GCC compiler (it is an integrated compiler of the GNU project for C). first we need to convert all .c files to .o object files.
We can compile each one separately (in case we don’t need to use them all), as an example we will use the following files, each one contains a function that is going to be added to the library-

Compile libraries files
$ gcc -c example.c -o example.o
Or all the files within our location in the directory
$ gcc -c *.c
-c - it means creating an intermediate object file, instead of an executable file.
In this case, they will remain with the same name and their extension will change.
We have compiled the library files, they will be left with a new extension -o (object) and we can continue with the creation of the library

Create static library
This step is to bundle multiple object files in one static library . The output of this step is static library.
$ ar - a Linux tool to create, modify and extract from files.
The command:
r — replace existing files within archive.
c — create a file if it doesn’t already exist.
s — create an object file index on the file.
$ ar rcs libholberton.a file_example_add.o

Once the library is created we can add the .o files, we can do it separately or by extension with * .o
$ ar rcs libholberton.a *.o
This step is finished with ar -t we can view the contents of the library
$ ar -t libholberton.a

nm — This utility will allow us to examine the functions contained within a library and the functions declared within it but not defined in it.
$ nm libholberton.a

Now we have the static library ready to be used as part of the compilation process and link when creating an executable program in c. We can do the test with any .c file that we need to compile with the following command using the gcc compiler.
$ gcc main.c -L. -lholberton -o main
- -l :library name without extension
- -L :esta bandera le dice al enlazador que las bibliotecas se pueden encontrar en el directorio dado.
- -o : place the output into
now we can run the ‘main’ file
$./main
Executables generated using static libraries are the same as executables generated from source files or individual objects. At compile time, linking to a static library is faster than linking to individual source files.
In static binding, the size of the executable becomes larger than in dynamic binding, since the library code is stored inside the executable instead of separate files.