WHY USE LIBRARIES? RELOADED.
Libraries.
As we create programs, we find that some pieces of code are repeated over and over again in our work and we may find ourselves at the point where an effective solution for this is needed, which does not involve copying and pasting or having to search for such. functionality, and it is there where we can use the libraries, whether static or dynamic, taking into account that each of these options has things in its favor and against it.
By creating a library, we can have a neater way to develop our projects, additionally it would save us way to get to the compilation.
these are some of its advantages:
1- not having to rewrite code or make copy and paste
2- The compilation times are shortened since the functions or parts of the program saved in the library are precompiled or in object file format.
3- The code that is ready is reliable since it will be tested over and over again and each time we do it we can correct errors.
How do we create a library?
Well, the first thing to create it is to have at least a .c file with the code of our functions and a header file .h with the types typedepfs, structs, enums and prototypes of the functions to be introduced in the library.
This is a simple example of a .h file which we have called library.h that you should have for creating a library.
And these are a couple of simple functions that add and subtract two integers.
Static and dynamic libraries.
In Linux we can use two types of static and dynamic libraries.
Statics are libraries that are copied into our program when compiled. When we do the compilation process of our program we do not need the library since the necessary copies are made in the compilation, the disadvantage of this is that our program will be heavier and also if we modify and / or update the library, our executable files, the latter has an advantage and disadvantage depending on the case, but the advantage is that our program will be faster when compiling and will have better portability since you will not have to copy the library when moving it from a computer to another.
The dynamics are libraries a little more docile and sometimes more practical when it comes to updating since these are not copied into the program when compiling it, which means that when you modify the library it will modify the result in your executable file, because each Once you use the file, you look for the libraries connected to it, which translates into a longer execution time, but makes your program lighter, which allows us to do work with a greater scope in the code, one of the most notable disadvantages It can be found in portability since without wanting to transfer your program to another computer, it will also be necessary to transfer the library.
Now let's see a small comparison between static and dynamic libraries.
1- The programs that are made with static libraries are larger since when compiling a copy of a part of it is made in our program, while with the dynamics these are kept separate.
2- Programs compiled with static libraries have great portability by not having to take the libraries to their new location.
3- A program compiled with a static library reduces execution times since when you need to use the library function, it is included in your code and you do not have to go looking for the library file as happens in dynamic libraries.
4- If a static library is changed it will not affect the executable file or already compiled program, in dynamic libraries the executables are affected when modifying the library so this acts as an advantage and a disadvantage, advantage because it allows you to update and correct errors, disadvantage because if you have a problem in your library, all your executables will have it when using them.
So what library do I use?
Well, it all comes down to the simple answer "the one that best suits the needs", taking into account the advantages and disadvantages, it all comes down to your criteria. But if you want my advice I would say that it would be better to use the static libraries in small and simple programs where you do not have problems with the amount of space they occupy, and the dynamics would be for huge programs and system libraries since these are on all computers thus making the copy of the gallery to our new computer unnecessary.
In unix the static libraries are usually called libfile.a and the dynamic libfile.so, where file is the name of the library that was created.
Compile with static libraries.
In this case I will leave the link to one of my previous articles that specifically talks about how to create a static library.
Create a dynamic library
We start with having the aforementioned .h and .c files, and we will use the command gcc * .c -c -fpic this command does the process of precompiling or creating object files for all files ending in .c found on the working desktop, the -fpic flag ensures that each file has a separate space, and the -c flag ensures that the object file is not linked.
Then you must put the command gcc * .o -shared -o liball.so, replacing all with the name by which you are going to call your library, when you put this in the command line you will take all the .o files with the wildcard * and With the shared flag you will organize them in a dynamic library, the names of the dynamic libraries must begin with the prefix lib and end with .so the rest of the name is up to you, finally we must import our library to a place where our compiler You can easily search for it and for that we use the command export LD_LIBRARY_PATH = $ PWD: $ LD_LIBRARY_PATH and so our library is ready to be used when we need it.
Now that the library is ready it is used as follows:
gcc which is our compiler followed by the -L flag that instructs the compiler to look for the library in the current directory, followed by the name of the file that contains our code to compile in this case is holberton_file.c, then the -l flag that tells the compiler to look for the dynamic library by its name followed by the flag -o and the final name of the executable, and the result would be like this:
gcc -L holberton_file.c -lholberton_lib -o holberton_exe, this would compile the file called holberton_file.c with a function contained in the libholberton.so library and would result in an executable file called holberton_exe.