C static libraries

The libraries are objects files, content symbols like functions, constants, and header. and can be used in the linking phase and faster to link to other programs.
Unix system allows us to create two types, static and dynamic or shared libraries.
STATIC
It is a collection of objects files and inserted into the executable file. So, only the executable file is necessary to run the program in everywhere. however, increment its size.
CREATE STATIC LIBRARY
1. We need a file .h to save all the functions:
holberton.h — functions
with files .c of functions

with file .c of each function into of .h file

2. Create Objects files with GCC:
With the .h and all “.c” files, we create objects files “.o” each this command:


3. Create Static Libraries:
There is a tool for creating a static library called ‘ar’ allow us to create and modify:

Some times is necessary indexed. in some cases for a modification like a new function etc. In this case, we use ‘ranlib’:

And now it is ready to use with the next command:

DYNAMIC LIBRARIES
It library are not into executing file so, is necessary to execute with the library. The format of Shared file or dynamic file is “.so”. Is is created of the same way that the static file.
- Need to have “.h” and “.c” file, the command is:
gcc -f PIC -c *.c
“Compile for “Position Independent Code” (PIC) — When the object files are generated, we have no idea where in memory they will be inserted in a program that will use them”. — docencia.ac.upc.edu-
Library File Creation
“It has a format that is specific to the architecture for which it is being created”.
gcc -shared libholberton.so *.o
And used :
gcc main.c -L. -lholberton -o main