Dynamic libraries
What is and why using libraries
A library is a set of functions and variables put together in such a way that by calling that library, its variables and functions can be accessed without having to define them. And this is exactly the main reason why to use them. We can access global or own functions without the need to define them in each file that we are going to use for our code. In this way, we can use, for example, the "printf" function simply by calling the library that contains it "stdio.h" instead of having to develop its functionality in our file. In this way we save a lot of code and improve the efficiency of our program.
How do they work
Every library consists of two parts: a header file and the code file. The header file, normally denoted by a ".h", contains information about the library and contains constants and types, along with prototypes for functions available in the library. There are two types of libraries: Static and Dynamic. Let's see how each of them works.
Static libraries: These types of libraries are static because they are copied to our code at the moment of compilation (in the last phase: linked) and become part of it. Once it is inserted it is historical like the rest of the code. That is, no matter how much the library changes after compilation, our code will not be modified, it will remain static with the versions of the functions that were in the library at the time of compiling. It is important to mention that only the parts of the library that our code calls throughout it are copied and not necessarily all the content of the library. This means that if we have a LIB library with two functions (a and b), and in our code we call the library and use the function b in it, in the compilation only the function b will be copied.
Dynamic libraries: On the other hand, dynamic libraries are not copied into our code when compiling. Only a pointer to the functions that are called in the code is recorded and when it is executed, they are accessed. This means that if the library is modified after the elaboration of our code, its performance can be modified since it is at the moment of execution that the information is sought. Consequently with this our same executable can have two different performances depending on whether there was a change in the called dynamic libraries.
We can see then that the main difference between these types of libraries is when our code accesses them and uses the information. In the static ones when compiling and in the dynamic ones every time the executable is run.
Not necessarily one type of library is better than the other, both types have their advantages and disadvantages:
How to create a library in Linux
A - Dymaic Libraries:
1- Put all .c files together in a folder
2- Generate for each c file its object file
3-Put all all objects files into one library
Lets see with an example 👀
Files: 2-funciones.c , 3-funciones.c , 4-funciones.c, 5-funciones.c
As we can see below, each file contains a group of functions inside:
We want to make a Library call libdynamic.so with all this functions inside.
Recommended by LinkedIn
First step: Put all the ".c" files together in a folder.
Generate for each c file its object file: For that use gcc with the option c and with fPIC for generate the position independent code.
gcc -c -fPIC *.c
Once you have the object files, they must all be put together in the library. In our case libdynamic.so For this we use also the gcc but with shared.
gcc -shared -o libdynamic.so *.o
Congratulations, Your library was created!
To call your library you just need tu put "nm -D" and the name of the library.
nm -D libdynamic.so ✔
Once you have created your library to use it, the first thing you should do is set the environment variable LD_LIBRARY_PATH. This way the system would know the location of yours files.
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
To use them in Linux the only thing you have to do is compile your mains with de opction -L and lydinamic.
gcc -L. main.c -ldynamic
B- Static libraries
To create a static library the process is a little similar
1- Put all .c files together in a folder
2- Generate for each c file its object file: CODE gcc -c *.c
3- With the "ar" put all objetc files togethers in a stat lib: CODE ar -rc lib.a *.c