STATIC LIBRARIES VS DYNAMIC LIBRARIES
As we make computer programs, we realize that some parts of the code are used in many of them. For example, we can have several programs that use complex numbers and the functions of addition, subtraction, etc. are common. It is also possible, for example, that we like to play games, and we realize that we are repeating the code over and over again to move an image (a little Martian or Lara Croft) across the screen.
It would be great to be able to measure those functions in a separate directory from the concrete programs and have them already compiled, so that we can use them whenever we want. The huge advantages of this are:
- Not having to rewrite the code (or copy-paste).
- We will save the time of compiling each time that code that is already compiled. In addition, we already know that while we make a program, we test and correct, it is necessary to compile between many and "more many" times.
- The already compiled code will be tested and will be reliable. Not the first times, but when we have already used it in 200 different programs and we have corrected the errors.
The way to do this is to make bookstores. A library is one or more functions that we have already compiled and prepared to be used in any program that we make.
In Linux we can make two types of libraries: static and dynamic.
A static library is a library that is "copied" into our program when we compile it. Once we have the executable of our program, the library is useless (in other words, it is used for other future projects). We could delete it and our program would continue working, since it has a copy of everything it needs. Only that part of the library that is needed is copied. For example, if the library has two functions and our program only calls one, only that function is copied.
A dynamic library is NOT copied into our program when compiled. When we have our executable and we are finished with it, every time the code needs something from the library, it will look for it there. If we delete the library, our program will give an error that it cannot be found.
What are the advantages and disadvantages of each of these types of bookstores?
A program compiled with static libraries is bigger, since everything it needs is copied.
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, in principle, faster in execution. When you call a library function, you have it in your code and you don't have to go read the dynamic library file to find the function and execute it.
If we change a static library, the executables are not affected. If we change a dynamic, the executables are affected. This is an advantage if we have changed the library to correct an error (it is corrected automatically in all executables), but it is inconvenient if touching that makes us change the executables (for example, we have added one more parameter to a library function , ready-made executables stop working).
How to create our libraries?
To create a static library please go to our previous blog where we explain step by step.
The steps to create the library dynamic are the same as for the static library.
let's see the steps to compile:
1. Compile the files that will be included in the library
The first step is to compile the files for the library using two flags -c
to get the object code .o
and the flag -fPIC
stands for Position Independent Code that tells the compiler that the code can be loaded at any particular virtual memory address at runtime.
gcc -c -Wall -Werror -pedantic -fPIC <files.c>
2. Creating the library
The next step is to create the library based on the object codes previously created, we use the flag -shared
that instructs the linker to create a shared object.
Every shared library has a special name called the soname. The soname has the prefix lib
, the name of the library, the phrase .so
, followed by a period and a version number that is incremented whenever the interface changes.
gcc -shared -o <name-of-library.so> <files.o>
Comentarios
Publicar un comentario