Differences between static and dynamic libraries
Cesar Molina
Software Developer Junior | JavaScript | React JS | HTML | CSS | Figma | Wordpress
The?main difference?between static and dynamic linking is that?static linking copies all library modules used in the program into the final executable file at the final step of the compilation while, in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.
Why use libraries in C?
Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use. Functions in a C library can be used and accessed by programmers to create several different programs.
?
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 functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.
How a library works in C?
From now on you should be familiar with the compilation process using GCC for a C file.
Four steps during compilation process:
During the last step, all functions from the library used in the compiling program are merged into one executable binary file. From here things diverge depending on the kind of library you are creating.
In a?static library, all the functions you had in your library are inserted into the executable binary file. This means that you are not able to change any function without going through the compilation process again of all your files (i.e. main file, other C files and your library files).
In a?dynamic library, all the functions you had in your library are not inserted directly into the binary executable file. Instead, it is inserted the address where the program can find the functions. Thus, you are able to make changes within the functions and just compile the library C files.
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. Some operating systems and processors need to build libraries from position-independent code so that they can decide at runtime where they want to load it 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 (Linux)
The point of creating a dynamic library is to use it with other programs. You can compile your code as follows:
领英推荐
?
gcc -L test_code.c -lholberton -o test_code
In the above command it is worth noting that your source code,?test_code.cin this case, needs to be listed before the?-l?flag. The expression,?-lcombined 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. This is why it is important to use the standard format for naming that I described earlier. For instance if?test_code.c?was the following:
#include "holberton.h"
?int main(void)
{
???_puts("Hello World!");
?????return (0);
}
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 have fun!!!
Advantages of Using C library functions
1. They work
One of the most important reasons you should use library functions is simply because they work. These functions have gone through multiple rigorous testing and are easy to use.
2. The functions are optimized for performance
Since, the functions are "standard library" functions, a dedicated group of developers constantly make them better. In the process, they are able to create the most efficient code optimized for maximum performance.
3. It saves considerable development time
Since the general functions like printing to a screen, calculating the square root, and many more are already written. You shouldn't worry about creating them once again.
4. The functions are portable
With ever-changing real-world needs, your application is expected to work every time, everywhere. And, these library functions help you in that they do the same thing on every computer.