The differences between static and dynamic libraries.

zitouni mehdi
3 min readDec 14, 2020

--

Why using libraries in general

How do they work

How to create them (Linux only)

How to use them (Linux only)

What are the differences between static and dynamic libraries

What are the advantages and drawbacks of each of them

Why using libraries:

when we work as a programmer with any program like C for example,

we use the same function or code multiples times. this consumes a lot of memory and a lot of times to compiler and so on…, to resolve this problem we build a library (static or dynamic) indeed it is best to put these fonctions in a library and use them to speed up the compilation of the program.

How do they work:

when you compile a source file you get an object file. in linux platform its extension may be .o

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries on the other hand, exist as separate files outside of the executable file. A Dynamic Link Library is a software library whose functions are loaded into memory by a program, if necessary, during its execution, as opposed to static or shared software libraries whose functions are loaded into memory before program execution begins.(Wikipedia).

When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. A linker can accomplish this task in two ways, by copying the code of library function to your object code, or by making some arrangements so that the complete code of library functions is not copied, but made available at run-time. (GeeksforGeeks)

How to create static libraries in linux:

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, .a files

1. Create a C file that contains functions in your library.

2. Create a header file for the library: for example holberton.h that contain different prototypes of functions.

3. Compile library files

gcc -c lib_mylib.c -o lib_mylib.o

4. Create static library

ar rcs lib_mylib.a lib_mylib.o

How to create Dynamic library

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 .

Next, type in the following command: gcc *.o -shared -o liball.so 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. finaly we generate a file libraries with .so extension.

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 -l holberton -o test_code 
static librarie
Dynamic library

--

--