Differences between static and dynamic libraries
Juan Sebastian Gonzalez
Software Developer | Full-Stack Developer | Javascript | React | Python | Nextjs | Nestjs | Fast Api | Docker | MYSQL | MONGO
A library is a powerful tool in a programmer's arsenal, consisting of pre-compiled functions that can be easily reused throughout a script. These functions are blocks of code that save time and effort by eliminating the need for repeated coding. In fact, leveraging existing libraries can significantly boost a developer's productivity.
Typically, object files are generated by the compiler when the "-c" gcc flag is used, and they follow a naming convention ending with ".o". These files contain function definitions in binary form and are used to build libraries.
At runtime or compile time, libraries play a critical role in software development. In C programming, two types of libraries are available: dynamic libraries and static libraries. By selecting the right type of library for a particular application, developers can optimize their code for performance, scalability, and maintainability.
Static libraries are compiled into a program, with a copy of the library's files held within the executable file. This results in a larger file size as the external programs are compiled as well. However, all components are stored within the executable file, making it resistant to vulnerabilities. Additionally, the object code being in the executable file results in faster runtime speed since function calls are executed more quicker.
On the other hand, dynamic libraries occur as separate files outside of the executable file. This results in a much smaller library size and allows multiple running applications to use the same library, providing significant advantages. Furthermore, dynamic libraries are linked at runtime, meaning that changes can be implemented without recompilation or relinking. However, since the library is outside the executable file, it is vulnerable to breakage, which would render the executable file unusable.
In summary, while static libraries offer security and faster runtime speed, dynamic libraries provide more flexibility and better resource management.
CREATION
First, is needed to have all the .c files and the header file in the current working directory or at least in a known and accessible directory.
Dynamic library creation (Linux only)
Write the command listed below to generate the object files (step 1):
gcc -fPIC -c -Wall -Werror -Wextra -pedantic *.c
Next, create the library by using.
gcc *.o -shared -o liball.s
As an alternative, it's possible to approach this in a single line by implementing the next line of code.
gcc -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o liball.s
o
The?compiler?will later identify a library by searching for files beginning with?'lib'?and ending with the naming convention,?.so
领英推荐
The program needs the path in order to look for the library files. So, is needed to type the next command to add that location to the environment variable called?LD_LIBRARY_PATH.
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
Or like an alternative:
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
This can be made also by sending the created library file to /usr/lib folder like as follow:
cp liball.so /usr/lib/
But is highly recommended to use the first or the second way.
Static library creation (Linux only)
You can follow deeper the flow to create static libraries in the next resource:
Creating a static library is much easier.
ar rcs liball.a *.o
Program compilation
gcc -L. -lall -o app main.c
When compiling program files, is mandatory to tell the compiler to use the library files and where to find them.
With this short explanation, I hope you have a better understanding of static and dynamic libraries.