Dynamic libraries
To start talking about dynamic libraries, we will remember what libraries are and what they are used for.
Certain types of files that we can import or include in our program are known as libraries or libraries. These files contain the specifications of different functionalities already built and usable, by being able to include these libraries in our executables we can save ourselves a great deal of work.
How do dynamic libraries work?
It is NOT copied into our program when we compile it. When we compile our program and are running it, every time the code needs something from the library, it will go to the library. If we delete the library, our program will give an error that it cannot be found.
How to create dynamic libraries?
1) To create a library of this type, we must first have all the .c files to use in the same directory, then execute this command that will create the .o (object code) files for them.
The -fPIC flag generates position-independent code for shared libraries.
- *.c wildcard which makes it apply to all .o files
2) Finally we will execute this command that will transform the object codes into a library with the name that we indicate after the -o
-shared is a flag that tells the linker to create a shared object.
- *.o wildcard which makes it apply to all .o files
领英推荐
How to use dynamic libraries:
To use the dynamic library that we created, you must add the location of your library in the environmental variable LD_LIBRARY_PATH so that the compiler can find the object code of the functions that they use, to do this, use this code
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
For example, if you want to use some functions of the created library in your code, you must instantiate it at compile time.
Differences between static and dynamic libraries
The differences between these two libraries is found when implementing their functions. In the static libraries the code of each function is introduced in the program used, in this way it is not dependent, on the other hand when using dynamic libraries the program calls the Library functions used at runtime.
What are the advantages and disadvantages of each of these types of bookstores?
Valentin Repetto - 2022.