Everything you need to know about libraries in Linux
According to the definition of the library: “A library is a curated collection of sources of information and similar resources”, and this is exactly what it meant to be in programming Linux.
So, a library is a collection of piece of code gathered in a single package which provide a reusable functions, routines, classes, data structures … and we could distinguish two types of libraries in Linux: the static libraries and dynamic libraries
Compiling process
In order to better understand the concept behind the Linux libraries we need to highlights the compiling process. it’s a four steps process provided by a compiler such as GCC “Gnu Compiler Collection” or CC “C Compiler”:
Pre-processing, compiling, assembling, and linking.

By illustrate the workflow of the compiler it’s more clear to see the libraries involving layer and first let’s see the Static libraries.
Static libraries
The static libraries or the statically-linked library which is a package of functions and routine call in compile time copied into the caller target by the linker producing an executable machine code, it’s a static build of the code in an archive. the construction of the static libraries is quite simple as archiving a bunch of pieces in a single archive.
Let’s assume the list of this functions using to manipulates strings
First we need to create the object file:
gcc -c strchr.c strchr.o
gcc -c strlen.c strlen.o
We know that’s the static libraries is a set of “object file” there wher copied in a single file suffixed by “.a” which created with the archiver “ar”
ar rcs stringlib.a strchr.o strlen.o
With the static library, we can statically link main.o
with the library. The -L
flag indicates (a non standard) directory where the libraries can be found. The -l
flag indicates the name of the library. Note, that it assumes the library to start with lib
and end with .o
(so lib
and .o
must not be specified)
gcc -o executable stringlib.a main.c
Shared library
These are linked dynamically simply including the address of the library. Dynamic linking links the libraries at the run-time. Thus, all the functions are in a special place in memory space, and every program can access them, without having multiple copies of them.
linking a shared library is quite similar to the previous static linked with different flag when producing object file such as “fPIC” = Position Independent Code
gcc -c -fPIC strchr.c strchr.o
gcc -c -fPIC srelen.c strlen.o
To create the shared library we need the “-shared” flag:
gcc -shared strchr.o strlen.o -o stringlibshared.so
Static library VS Shared library
- The dynamic library needs to be packaged with the executable and must be available when the executable starts running (more specifically, calls a function provided by the dynamic library)
- A static library is only needed during building the executable (during link stage). It is not needed to run the executable because the library code is embedded inside the application.
- Disk space efficiency: The same dynamic library can be shared among multiple executable on disk. However static library if each application only uses a small fraction of the total static library, the disk space efficiency can still be competitive with a single large DLL.
- Versioning problems: You might run into issues when a version of the dynamic library used by your application conflicts with an older/newer version of the library present on the operating system. Since all the library functionality is linked into the application, it does not amtter if other applications on the system is using a different version of the static library.