Static libraries
Andres Castaneda
Software Developer | Python | Django | DRF | Flask | AWS | Git | coding enthusiast | continuous learning and professional development | incorporating new technologies and methodologies | 4 years of experience
A library is a file that contains multiple object files, which can be used as a single entity in a linking phase of a program.
how do they work?
Static libraries are just collections of object files that are linked to the program during the linking phase of compilation and are not relevant at run time. The static libraries are the way in which all the functions that our project may have are grouped, with the purpose of improving compilation, links and improving the order in our project.
Creation
Steps to consider:
1) have .c files:
it is necessary to have the files that we want to add to the library; example:
main.c
_putchar.c
holberton.h
...
2) Convert files *.c to *.o:
we do this with the command:
gcc -c *.c
this command generates the * .o file (Compile and assemble).
This command generate the result of this command is:
main.o
_putchar.o
holberton.o
...
3) Static library creation
use the command:
ar rc <name-new-library>.a *.o
With this command, all files of type * .o are added; <name-new-library> is the name of our new library, with extencion .a (example libstatic.a).
4) Index static library
we need the command:
ranlib <name-new-library>.a
This command is to index the library, it facilitates the reading of the functions.
We want to use it in a program. This is done by adding the library name to the list of object file names given to the linker, usually using a special '-l' flag. Here is an example:
cc main.o -L. -lutyl -o prog
This will create a program using the object file "main.o", and whatever symbols it requires from the static library "util".