How to create and use dynamic libraries
Why use libraries in general?
The computer libraries are a set of functional implementations encoded through a programming language that offers a defined interface for a specific functionality.
Libraries are used by other independent programs and simultaneously, they are usually linked to a program or other library at different points of development or execution.
How do the libraries work
In order for a library to function properly, the staff, place, collection, to the automation of the library must be taken into account.
In the market there are different alternatives for the management and creation of a library, either proprietary or free software.
Libraries are responsible for extending the functionality of a conventional library, it is any application or application system aimed at providing access and service to large volumes of information, which must be organized and digitized.
How to create a dynamic library
We need to start creating the object files first with command gcc -fPIC -c *.c
As you have noticed, this time we created the object files with the -fPIC
flag. This flag stands for Position Independent Code, a characteristic required by shared libraries.
On the next step we will create the library named
gcc -shared -o liball.so *.o
The -shared
key tells the compiler to produce a shared object which can then be linked with other objects to form an executable.
As the result, we’ve got the library that is ready to be used.
How to use the dynamic library
The program needs to know where to look for library files. Therefore, we have to add the location of the dynamic library to the environmental LD_LIBRARY_PATH.
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
To use this dynamic library we would create a program, let’s call it main.c. This program uses functions from our dynamic library.
gcc -liball -L. main.c -o main
After having created this new shared library, we can use the ldconfig command to create the necessary links and cache
What are the differences between static and dynamic libraries
The difference between a static library and a dynamic one is that in the first one it is “copied” in the program when it is compiled, followed by that once the program executable is available, the library is used for future projects, because it is deleted as follows working since you have a copy of everything you need. While a dynamic library is not copied into the compiled program, that’s why when you have the executable and it is running every time the code needs something from the library, it will search for it to bring it; That is why this library cannot be deleted because otherwise our program will give an error that it cannot find it.
What are the advantages and disadvantages of each of them
Advantage:
- A static library is larger, since it copies what you need.
- A program compiled with static libraries can be taken to another computer without having to take the libraries
- A program compiled with static libraries is faster in execution
- If a static library is changed, the executable is not affected.
- If a dynamic library is changed to correct an error (this is automatically corrected in all executables)
Disadvantages:
- If a dynamic library is changed, executables are affected.