C Static Libraries
Let's start with a simple definition: what is a library? It is a collection of functions. Unlike an executable program, the behavior of a library does not expect to be detected autonomously. Its purpose it to be used by other programs, independently and simultaneously.
Libraries contain the object code of many programs that allow us to do common things, such as reading the keyboard, performing mathematica functions, and others. They are classified by the type of word they do, there are input and output libraries, memory management, etc, all of them with a specific function.
We use libraries because they store functions that other people have written before us, archiving a large amount of work.
How do Libraries work?
Libraries work by including them in the first part of our code:
In the linker stage of compilation (you can read more about this in a previously blog), the binary code of the called library functions is inserted and executed by the operating system. There is a trusted path that te compiler follows to find libraries: is/usr/lib and /lib.
Now, before we continue, the difference between calling or inserting a function's code is what differs static from dynamic libraries.
Creating and using a Static Library
- Create a header file: in the same directory where you have the C functions.
2. Compile all the .c files in the current directory:
gcc -Wall -Werror -Wextra and -pedantic -c *.c
3. Now, we get all the .o files (object codes), but in order to take them all into a file we need the next command:
ar -rc libholberton.a *.o
where: libholberton.a is the library, ar stands for archive, the r flag means that the library replace the older object files with the new ones in the library, and the c flag tells the command ar to create the library if it doesn't exist.
Finally, if you need to index it, you can use the next command line:
ranlib libholberton.a
So, we just create a static library.
Lilibeth Tabares.
--
4yHola Lilibeth ... muy interesante