Why use libraries in general?
Would you create a building in a single block and place it in place?
I hope not!
For the creation of these structures, small parts such as pre-built bricks or wall blocks are made.
All these parts will make a great building that will be supported on our foundations and operated by its skeletal structure.
In this small analogy it would be the construction of programs, instead of a single block of code compiled with their libraries, it is better to create the programs in small parts that can be called when needed and can make modifications without doing major operations.
A library is a compilation of other files, pieces of code in itself, to be reused in a program, being simpler to use functions that are used several times when required, most of the time containing a specific group of functions.
Libraries are of two types:
Dynamic libraries and static libraries.
In a static library the library code will be included in the compiled executable file, this indicates that if there are several libraries, they will be included in the main code making the file very large, can you imagine if they have 200 libraries? Another thing that happens with static libraries is that when making changes to them, it is not very easy to perform an update because all the files that generated this large file are required for an easier update. Imagine an update of an important file where a change is needed in one of your libraries, it would have to be recompiled, resulting in a complete change in the main file, besides being risky it is not functional.
On the other hand, in a dynamic library, the files remain separated from the main code, making it not a heavy encapsulated code, loading the library files into memory and connected to the main code by links to memory, making this library usable when need in the code, if it is in memory to execute it or if it is not, loading it when necessary, now in the case of updates, it is easier to change the file of a library already compiled and that will be used normally by the main code.
To create a static library check my blog where I describe the process
It’s not just static libraries in the city.
Not only in the common life there are libraries to read our books, but also in the programming field they exist, where…
To create a dynamic library in a few words the following is done:
Put all the .c libraries in the same folder and apply the following command:
gcc * .c -c -fPIC
create the code that is independent, so that several programs use the same shared library.
The “-c” option stops the compilation process on the linker by creating .o files
to generate the .o files of their respective file in .c and after this the compiled file of the packaged libraries must be created with the following command:
gcc * .o -shared -o liball.so
in gcc *.o, taking all the .o files
-shares: where it indicates that it is shared and
-o to compile .o objects to a .so file
liball.so is an example of a name
To use the libraries
It is good to keep in mind that what has been done is for Linux environments, now we are going to assume that the main of our code will be called myprog.c, and as a result of the compilation we will call it progresult
When compiling, to see what shared library dependencies the program has, use the ldd command, as an example:
if it returns
liball.so => not found,
it implies that the library was not found by the loader, so we must place it in a standard location.
For this, the environment variable LD_LIBRARY_PATH must be updated as follows:
export LD_LIBRARY_PATH = .: $ LD_LIBRARY_PATH
There is another command to verify the symbols of a dynamic library, this is called nm
$ nm -D libmylib.so
to obtain information from the libraries the flag -D is placed
Importantly, if changes are made to library structures, be they additions, removals, or modifications, consider the ldconfig program that configures symbolic links in dynamic link directories to have security on the correct updated links.