What are the differences between Static and Dynamic Libraries in C and how to use them?
To be in context, when it is coding the use of functions is an essential tool to use, it helps us to create a block of statements that solve a specific task and most of the times we can reuse this functions just calling them when we need it, that method is a better way to organize our code and it is not necessary to be writing every time the same block of code, there is where the libraries are the next tool to implement on the projects, those are a way to save all the files where we developed our functions and link all of those on a single file, that's why its named library.
Libraries Features
- Libraries are a simple and versatile way to modularize and reuse code.
- A library is essentially a set of functions and/or procedures.
- In order for this encapsulated functionality to be used, the program that requires must be responsible for linking the library that contains it.
Type of libraries
Static libraries
Static libraries are a set of object files that are bound to the program during the binding phase of compilation, and at runtime are not that important because the object files are also used only during the binding phase, and they are not required at runtime only the program executable file is required to run the program. When compiling a program and in this way, the library with the compiled source code of the program remains in a single executable file.
The extension of the libraries file on Linux is .a
Dynamic Libraries
They are linked to the program in two stages. One is, during compilation time, the linker checks that all symbols such as function variables, among others, that are necessary for the program, are linked to the program or in one of its shared libraries. But, the dynamic library object files are not inserted into the executable file. Instead, when the program starts, it checks which shared libraries were linked to the program, loads them into memory, and attaches them to the copy of the program in memory.
The extension of the libraries file on Linux is .so
Static libraries vs Dynamic Libraries
Advantages of a static library:
- The functionality to be used by the program is only copied if the library has 10 functions, but the program only uses 2, it is only those 2 that are copied.
- They allow faster execution, since the executable code of the functionality is found along with the program executable
Disadvantages of a static library:
- The final executable code is larger, because of the functionality.
- A change in the fonts of a static library, forces to recompile the programs that make use of the functionality provided by it.
Advantages of a dynamic library:
- The final executable code of a program that makes use of a is smaller, because of the functionality of the bookstore in this.
- Changes made over a dynamic library do not force to recompile each of the programs that use it.
Disadvantages of a dynamic library:
- They can provide slower execution, since the executable code of the functionality is found in a file outside the executable of the program that uses it.
- Programs must necessarily be moved together with all dynamic libraries you use.
How to make a static library
In the main program, it needs to include the header file a file that includes a list of all standard libraries, user-created libraries, and prototypes of the functions used that will be included in the library, after that the files that will be part of the library must be compiled into object files with the GCC option -c that executes the first 3 steps of the compilation process preprocess, compilation an assembly, it generates an object file with the extension .o that are files with pure machine language (binary files).
~$ gcc -c function.c -o function_a.o function_b.o
The basic tool used to create static libraries, modify object files, list the names of object files in the library is a command called 'ar'. Then, you can create the library with the following command:
~$ ar rcs library_name.a function_a.o function_b.o
Flag 'r' means that any object files that already existed will be replaced, 'c' means that the library will be created and 's' will create an index file with the object files. If you did not create this index, you can create it using the 'ranlib' command.
To use a library in a program, you can use this command:
~$ gcc program_name.c -L library_name.a -o main
'-L' is the flag that links the library and the file where the functions are going to be used and the -o flag redirect to the executable file.
How to make a dynamic library
Similar as the static library first it needs to include the header file a file that includes a list of all standard libraries, user-created libraries, and prototypes of the functions used that will be included in the library, after that the files that will be part of the library must be compiled into object files with the GCC option -c that executes the first 3 steps of the compilation process preprocess, compilation an assembly, it generates an object file with the extension .o that are files with pure machine language (binary files) but we have to add the -fPIC flag in this case.
~$ gcc -fPIC -c * .c
Where '-fPIC' stands for position-independent code, which is a requirement of dynamic libraries, and '-c' creates an object file.
Then all .o files are compiled using the '-shared' flag. Later, when you compile program files, the compiler identifies a library by looking for files that start with "lib" and end with a library extension (.so for dynamics).
~$ gcc *0 -shared -o liball.so
Then export LD_LIBRARY_PATH = $ PWD: $ LD_LIBRARY_PATH is used, so that the program identifies where it should look for the different files in the library and that location is added to the environment variable LD_LIBRARY_PATH.
To configure dynamic linker runtime links, we can use the 'ldconfig' command with the '-n' and '-L' flags and if it is necessary to know what functions a library has, the 'nm' command should be used.
And for the last step, with this command (List of dynamic dependencies), it is verified if the linker can find the library:
~$ ldd prog
As mentioned above, this library is easy to update and the executable does not need to be recompiled as it is only linked at run time.
To conclude this article introduces or remembers the concept of static and dynamic libraries in C, as well as some advantages and disadvantages, how they work, how to create and use them.