Static and Dynamic libraries
A Library is a set of functions. It's like a program, only it doesn't have a function main(). It contains typical functions to be called from some other program or library. Any developer can create a new library.
Why using libraries in general and How do they work
- Static libraries. Bind when compiling, are left "inside" the final executable. In Windows they have the extension .lib. In Linux they have the extension .a.
- Dynamic Libraries. They bind when running, the operating system must find them when running the program. If an application was installed correctly, the operating system should have no problem finding it. In Windows they have the extension .dll. In Linux they have the extension .so (usually in /usr/lib o /usr/local/lib.
A library can be pre-compiled, or can you give us the sources. If you give us the sources, we must compile it before.
To use a library we must have.
Headers: The headers are the files with extension .h which contain the ''prototypes'' of the functions.
The compiled library: The compiler must know where the static libraries are. In ''gcc'', the flaq is used -L to define the route, and -l for the name of the library without the prefix ''lib'' and without the suffix ''.a''.
¿What are the advantages and disadvantages of each of these types of libraries?
- A program compiled with static libraries is larger, since everything you need 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 read the dynamic library file to find the function and execute it.
- If we change a static library, executables are not affected. If we change a dynamic, executable affected. This is an advantage if we have changed the library to correct an error, but it's inconvenient if touching that makes us change the executables (for example, we have added one more parameter to a function library, ready-made executables stop working).
What kind of library do I use then?
For programs not very large and for simplicity, I usually use static libraries. The dynamics are fine for huge programs or for system libraries, that since they are in all the computers with linux, it is not necessary to take them from one side to another.
In unix static libraries are often called libName.a and dynamics libName.so, where name is the name of our library
How to create a static library?
*. First we need to compile our C files into object code:
gcc -c *.c
*. Creating the library (.a). To do this use the command ar with the following parameters:
ar -rv libstatic.a *.o The option -r ar says you have to insert (or replace if they are already in) object files in the library. The option -v is to display information while you are doing things.
or
ar rcs libstatic.a *.o
Indexing the library
ranlib libexample.a.
We can view our recently generated index
nm libstatic.a
The Nm command provides more information about the symbols in our static library including the nm command will show us the virtual address of the symbol, If the symbol is lower case, it is local, if it’s written in uppercase then it’s external.
To use this static library we would create a program called pepegrillo.c that used functions from the library we just created. To use the library functions, we need to tell the compiler where to look.
gcc -L. -lall -o pepegrillo.c
How to use them
First we compile the program:
$ gcc -c -o apli1.o apli1.c
bearing in mind that compilation must be in route files interface (.h) Seller. Otherwise the compiler where these files are located must be shown by option -I, Something like:
$ gcc -c -o apli1.o apli1.c -Idir_lib
After the program is installed with the library indicating where it is and what is his name:
$ gcc -o apli1 apli1.o -Ldir_lib -lfich
- In the example it assumes that the library and its interface file in a directory called dir_lib.
- The option -I It is used to indicate which interface files are.
- The option -L It serves to indicate the directory where the library.
- The option -l It is to indicate the names of the libraries that are to be used, but should not be written nor the lib prefix or the .a extension because the compiler expects the aforementioned naming rules are followed.
How to create a dynamic library?
Compiling the sources, as before, to obtain the objects. gcc -c *.c
Create the library with the command ld.
The options for this command would be
ld -o liblibreria.so object1.o object2.o ... -shared
The liblibreria.so -o option tells the name you want to give the library
The option -shared It tells you to do a bookstore and not an executable
object1.o, object2.o ... are the object files that we want to put in the library.
How to use them
To use this library from a program not to do anything extra; It is exactly as in the case of static library.
By using a library, the compiler first looks for a dynamic version (.so), if not found then searches the static version
If you have two versions of a library and want to use the static version must be indicated to the editor flag -static.
The program needs to know where to look for library files. Therefore, we must add the location of the dynamic library to the environmental LD_LIBRARY_PATH. We could do this with the following command:
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
Version of a dynamic library
One of the great advantages of using dynamic libraries, apart from having smaller executable files, we can change the implementation of the libraries without having to recompile programs.