So to start, we need to know what a library is. A library is a set of functional implementations. is a collection of items that you can call from your program? A library is precisely like an executable, and the library’s functions are invoked with or without parameters from its executable.
Unlike an executable program, the behavior that a library implements does not expect to be used autonomously, but rather its purpose is to be used by other programs, independently and simultaneously.
Why we need to use libraries?
Libraries allow us to store function definitions that will be use in our executable program, as we said in the description before. Pprovide the user the benefit to use a variate of files that can be reused in different programs. Libraries are made of object files, with the extension ‘.o’, and can be made on Linux with the gcc compiler.
How libraries work?
Every library consists of two parts: the first parts is the header file that ends in .h contains all the information about the library that we are using, functions names, modules, directives, etc. The second and not less part is and the actual code file. A library is just like an executable, and the library functions are invoked with or without parameters from your executable.
The static libraries are just collections of object files that are linked to the program during the linkage phase of compilation, and are not relevant at runtime, while the dynamics link when executing. If you want to know more about it, I will let you the article I wrote days ago about static libraries and how to create them STATIC LIBRARIES IN C
Anyways, we are going to give a little step by step of how to create a static library, for more details go to the link I left before
- Convert all source code you want to save as a library, into object files:
gcc -c *.c
2. Use the [ar] “archiver” program and call all the object file “.o”.
ar -rc libholberton.a *.o
3. Finally compile:
gcc main.c -L. -lholberton -o main
they have the suffix .so. they can be linked to a program at run time. They are loaded at runtime and are NOT included in the executable, which reduces the size of the executable files
They exist as separate files from the executable file. When you compile a program that uses a dynamic library, the library is not part of your executable.
So, how to create a dynamic library?
1. To start we should create the object files first with command:
gcc -fPIC -c *.c
“pic” stand for position independent code
So as you can see in the img we have created the object files with the –fPIC flag, that is a characteristic required by dynamic libraries.
2. Now we are going to create the dynamic libraries, and to so this we need to use the flag –shared. This flags tells the compiler to produce a shared object which can then be linked with other objects to form an executable. We call all the objects that ends with. o ¿, and by convention a library should start with “-lib” and end with “.so” (extension shred object) for the case of dynamic libraries.
gcc *.o -shared -o libholberton.so
now we have created our dynamic library:
3. Now we can compile our programs using our dynamic library libholberton.so, running the next line command:
gcc 0-main.c -L. -lholberton -o len
our main.c file contains the next lines of code:
And we would get:
Useful commands to work with libraries:
- nm: we used nm command when we need to find out what functions a library has, as we can see in the image
- LD_LIBRARY_PATH and ldd: LD_LIBRARY_PATH. It’s an environment variable, and contains a set of directories where libraries should be searched for first, before the standard set of directories. It is usefull in this case for Configure the loader where to find the shared library. and ldd is a command we used to list the dependencies of a library. The env variable and the command has to be use like you can see in the next image:
To finish lets gonna show some differences between static and dynamic libraries:
· They work from within the executable.
· Depends on the compilation to be modified.
· Make a static linking at compile time.
· Are faster. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.
·Increase the overall size of the binary, but it means that you don’t need to carry along a copy of the library that is being used.
· Reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small.
· Work from outside the executable.
· We can use dynamic libraries in several programs
· It doesn’t depend on the compilation to be modified.
· Make a Dynamic linking of shared at run time.
· no need to recompile the executable.
Thanks for reading!
En informática, una biblioteca o, llamada por vicio del lenguaje librería (del inglés library) es un conjunto de…
Using Dynamic Libraries
There are two types of library in the ChorusOS operating system: Static Static library names are suffixed by .a. A…
Shared libraries with GCC on Linux
Libraries are an indispensable tool for any programmer. They are pre-existing code that is compiled and ready for you…
Difference between Static and Shared libraries - GeeksforGeeks
In programming, a library is a collection of pre-compiled pieces of code that can be reused in a program.