Differences between static and dynamic libraries
For this blog, I would suggest first take a look at this old entry I made months ago about static libraries https://andresgfranco.medium.com/what-in-the-world-are-static-libraries-d523a2fbccc9
Now that you are back, let’s continue and tell you that the wheel is already invented. That’s why we use libraries in C language when programming, so we can use functions or commands that already exist. We can trust and re-use those functions anytime we need them, mostly if they are already included in our GCC compilator or C language version.
But libraries are not divided between the ones that are already created and the ones we create. Conceptually and functionally, libraries are divided into static and dynamic. You will find the difference at the moment where they are “called” or used to make the program work.
Why using libraries in general?
Static or dynamic, how libraries will help you is to optimize, organize, and make a faster code. Since there are builtin functions that come with the language and there will be some other functions that you will create, you would like to reuse those functions in the future, so you don't have to waste time typing your code again or even worst, thinking about a solution you already think of, that’s time you invested, let’s keep using that investment in the future!
To reuse and keep using those functions, you will have the libraries, where you can store and call them to reuse the functions anytime you need.
But, how do they work?
As mentioned, they work at different times in the compilation process.
Let’s take a look at the following picture:

To build an executable file of a program these are the steps the GCC(GNU Compiler Collection) program follows:
- Preprocess
- Compile
- Assemble
- Linking
As a result, we will have an executable code. The static libraries are already included here, all we have here is a binary and interpreted code that will do whatever instructions we gave them before this process.
But, sometimes, after we have ready our execution file, in the running process we will need to include the dynamic libraries, that’s why they are called “dynamic” since they will be included anytime the program is running and we are calling them, otherwise they have not been included already in the executable file, unlike the static libraries.
How to create them
These instructions will be just effective in Linux OS.
To create a static library: https://andresgfranco.medium.com/what-in-the-world-are-static-libraries-d523a2fbccc9
To create a dynamic library:
- First, we will need to have our .c files ready. Once we know which file(s) are going to be used we can:
```$ gcc -fPIC -c [.c files]```
- As a result, we will have .o file(s) according to the c file(s) we included in the above command at the end.
- Now, having these files we will proceed and create immediately our dynamic library
```$ gcc -shared -o libraryname.o [.o files]```
How to use them?
Static library: https://andresgfranco.medium.com/what-in-the-world-are-static-libraries-d523a2fbccc9
Dynamic library:
- To use a dynamic library, first we would need to be at the same directory where the library is located. Then export it to the LD_LIBRARY_PATH environment variable by:
```$ export LD_LIBRARY_PTH=$PWD/libraryname.so```
- Now we should include it on the compilation process like this
```$ gcc -Wall -pedantic -Werror -Wextra -L -l[nameoflibrary]```
Pros and cons
The main pro of using a dynamic library is that you can do some maintenance to the code and reuse it in the future without the need for recompiling. It might be a bit slower in the execution but they “weight” less.
On the side of static libraries, the main pro is that it “weighs” more but it will have a faster execution since it is already compiled within the executable file.