Static libraries in C
Sebastian Carvajal
Comunicador organizacional y socio estratégico. Transformación y Cultura Organizacional | Gestión de Cambio | Endomarketing | Communicación | HRBP
The goal of this article is to explain what static libraries are in C, how to create them, and how to use them so that new C programmers can get the most out of static libraries.
What are static libraries?
We must first understand what bookstores are. Libraries are an archive containing several object files that is used as a unit in the linking phase when a program is compiled. These libraries index all the symbols (variables, functions and so on) thus allowing the program to access them more easily and quickly.
A static library is characterized in that it is not used during program execution. Quite simply, object files are linked during the linking phase of program compilation, so they become part of the executable program.
In contrast, a dynamic or shared library runs in two phases. In the first phase the linker verifies that all the necessary symbols in the program are linked or that they exist in the dynamic libraries. The latter are not included in the executable program. And in the second phase, when the program runs, the dynamic loader checks the shared libraries that were linked, loads them into memory, and attaches them to the in-memory copy of the program.
While this means a small delay when first running a program with a dynamic library, other programs can later use the same copy, saving memory.
How to create a static library?
Before creating a static library it is necessary to have the object files (.o) of our executable (.c) files. Therefore, we can use the following command to compile all the .c files in our directory and create their .o copies:
gcc -c *.c
With the .o files, we can create the library with the ar (archiver) command, in the following ways:
ar rc libname.a file1.o file2.o file3.o (indicating which files to include)
ar rc libname.a *.o (to include them all).
领英推荐
The r flag is used to replace old object files with newer versions, and the c flag tells you to create the library if it doesn't exist.
At this point, if we want to check the object files that are included in the library we can use the command:
ar -t libname.a
And the list of the symbols contained in the object files with this command:
nm libname.a
Once the file libname.a has been created it is necessary to index it to speed up the search within the library. The command we will use is the following:
ranlib libname.a
How to use a static library?
Once the library is created, when we go to compile our program (main.c) that will include the library, we will use the following command:
gcc main.c -L. -lname
It is important to note that the -l flag sets the library name without the lib prefix and without the (.a) ending. The flag -L. we use it to indicate that the library is in the current directory.
Following the previous steps we can create a static library and use it in our programs without having to write all the functions, variables and other elements that we need to run them. I hope you liked it and the process has been clear.