Static and Dinamic libraries in c
As a programmer, you may find yourself using the?same function or functions repeatedly. In this case, it is best to put this function or function in a library to accelerate the compilation of the program, also making this process easier. C libraries store files in object code; during the linking phase of the compilation process files in object code are accessed and used (.o files).
Static vs Dynamic libraries: differences and advantages/disadvantages of each.
Static libraries are reusable in multiple programs but are included in a program at compile time. Dynamic libraries, on the other hand, exist as separate files outside the executable.
The disadvantage of a static library is that its code is locked into the final executable and cannot be changed without recompilation. In contrast, a dynamic library can be modified without the need for recompilation.
Since dynamic libraries reside outside the executable file, the program need only make a copy of the library files at compile time. Using a static library, on the other hand, means that every file in your program must have a copy of the library files at compile time.
The disadvantage of using a dynamic library is that a program is much more prone to errors. For example, if a dynamic library becomes corrupted, the executable may stop working. A static library, on the other hand, is untouchable because it resides inside the executable file.
How to create a dynamic library in 3 steps:
Let's suppose you want to create a dynamic library called "dynamic"
1) First step:
First of all, compilate all the c files that you want to include in the library by using the following command:
gcc -fPIC -c *.c
This will compile every .c file and also, by using the -fPIC (PIC stands for Position Independent Code) and If supported for the target machine, emits position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on AArch64, m68k, PowerPC, and SPARC.
2) Secondly
After compiling all the .c files we had they will be converted to object files with the .o extension. What follows:
gcc -shared -o libdynamic.so *.o
By convention, all library names should begin with the prefix "lib". So our library will be called libdynamic.so, where the suffix .so stands for Shared Object.
领英推荐
3) Finally
Set the environment variable for the dynamic library
The dynamic linker needs to know where to find the dynamic libraries, so we create an environment variable LD_LIBRARY_PATH adding the path to the dynamic library, in this case, it will be located in the current directory.
How to use the library we just created?.
gcc -L 5-file.c -lall -o test_code
Let me explain you what this set of command does.
Your source code must be located by the -l flag. This flag tells the compiler to look for a dynamic library called liball.so.
It is crucial to write the library name exactly before the -L flag, for example if our library is called libdinamic.so, type dynamic before the flag.
Hope you enjoyed reading and learned smth knew. Thanks 4 reading.