Using and Creating Dynamic and static Libraries .
Ana Maria Roman
DevOps Engineer | Cloud Engineer | AWS | Women Who Code Medellín | DevOps Medellín ??????
What is it and why use libraries?
It is a compilation of the prototypes of functions that we use in our programs, the functions in programming language C, are declared and stored in a library, to be used at the time we need them, without the need to be copying the code every time this happens. This saves a lot of time when executing a program according to its complexity, accelerating the compilation process, additionally when working in teams this allows easiness in the tasks that are developed, easy reading and better management of the code, here the importance of libraries is evident.
How do they work ?
Static libraries, unlike dynamics, gather object files into one, while dynamics exist as separate files outside the executable, which means that static libraries add those files as they link, before it becomes an executable binary and dynamic libraries also have object files but are not added during compilation, they are kept separate from the executable binary, they are added to memory so that during execution these object files we need are kept available (linking process, final step of compilation).
The static libraries do not change while until we edit the functions contained in it, the dynamic libraries can at the time of compilation add functions during execution and this will not affect its operation, as previously mentioned, there is no need to compile again and again the program with the dynamic libraries.
How to Create a Dynamic Library (Linux)
To create a dynamic library in Linux, simply type the following command: gcc *.c -c -fPIC and hit return. This command essentially generates one object file .o for each source file .c . The -fPIC flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory. The -c options just ensures that each .o file isn’t linked yet.
Next, type in the following command: gcc *.o -shared -o liball.so (substitute your desired library name with all) and hit return. The wildcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag. The naming convention for dynamic libraries is such that each shared library name must start with lib and end with .so . Other than that though, let your imagination run free when considering names for your dynamic libraries.
Finally, we’ll need to export the path for libraries so that programs know where to look for them by executing the following command: export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
Using Dynamic Libraries (In Linux) .
In the command it is worth noting that your source code, test_code.cin this case, needs to be listed before the -l flag. The expression, -l combined with holberton tells the compiler to look for a dynamic library called libholberton.so, while the -L flag tells the compiler to look in the current directory for the library file.
gcc -L test_code.c -lholberton -o test_code
For example typing and executing gcc -L test_code.c -lholberton -o test_code would generate an executable file called test_code. In order to accomplish this, the compiler looks through the library that is specified with the -l flag for the _puts function object code. Executing test_code like so: ./test_code would give us the following output: Hello World!. Now that you know how to create and use dynamic libraries, go and conquer the world!
#include "holberton.h" int main(void) { _puts("Hello World!"); return (0);
}
How to use them?
To use a dynamic library, it is needed to export the LD_LIBRARY_PATH environment variable, which has a value of the first searched libraries by the executable file:
$ export LD_LIBRARY_PATH=$PWD/libcalc.so:
As shown, the library is on the Current Working Directory — $PWD. Then, the entry point — main.c— is compiled with the created library:
$ gcc -Wall -pedantic -Werror -Wextra -L. 0-main.c -lcalc $ ls calculator.c calculator.o header.h libcalc.so a.out main.c $
With -Lflag the directory where the created library resides is indicated. On the other hande, with-l flag, the name of the library is indicated to the compiler.
At the moment that a.out — the default name given by the compiler — is runned, this executable file finds the address of calculator and prints the result:
$ ./a.out 4 + 2 = 6 4 - 2 = 2 4 * 2 = 8
4 / 2 = 2
Software Engineer | Electrical Electronics Engineer
10 个月A lovely article Ana. I have a better understanding of the compilation flags and other intricacies involved in the creation, compilation and use of dynamic libraries, which could prove of great utility when writing Makefiles. Gracias Senorita