Dynamic Libraries
What is the library?
A library is a collection of precompiled routines that a program can use. The routines, sometimes called modules, are stored in object format. Libraries are particularly useful for storing frequently used routines because you do not need to explicitly link them to every program that uses them.
Why use libraries?
A library is good tool in the programming world because it’s are much advantages how the reuse the code as easier use the code already created and also climber the code in the future.
How they work a library?
The libraries are one tools of the compiler. A library is made up of several object files
What is the Dynamic library?
The resources occupy a separate file from the executable, which can be used by any application that needs it. Where this only saves the called functions and not the whole file
What is the Static Library?
denominadas también librerías-objeto (en relación a que sus componentes o módulos incluyen ficheros de este tipo), son colecciones de ficheros-objeto agrupados en un solo fichero, generalmente de extensión .lib o .a., acompañados de ficheros de cabecera, generalmente .h, que contienen las declaraciones de los objetos definidos en la librería. Posteriormente, durante la fase de enlazado, el linker incluye en el ejecutable los módulos correspondientes a las funciones y clases de librería que hayan sido utilizadas en la aplicación.
How to create a Dynamic Library
- create a Dynamic Library is to take all of the .c files we want and turn them into .o files by using
$ gcc -Wall -fPIC -c *.c
.
The flags
-Wall
and-fPIC
are used to help us and the Linker.
- Now all of the .o files need to be injected into a library. The following command is used to accomplish this:
$ gcc -shared -o library.so *.o
Connect Dynamic Library with executables
The steps for Connect our Dynamic library with:
- Compile the project with this form:
$ gcc -Wall -pedantic -Werror -Wextra -L. 0-main.c -llibrary -o program
Note:The
-L.:
flag was used to tell the linker that libraries might be found in the current directory(“.”)
-l(library name):
is the notation for including a library in the command line.
-o:
is used to name the executable “program”.
One last check with ldd
will show you that the library is now connected:
How to use a Dynamic Library
Now that the library is up and running, all you have to do is use the executable by typing:
$ ./program
"output"
And that’s it! Now you can use Dynamic Libraries as much as you want!