Differences between static and dynamic libraries

Ronaldaguirre
3 min readSep 8, 2020

It is simple! A library is nothing more than a set of ready-to-use functions for example:

library = “#include <string.h>”, function = “strlen()”

why should we use or why libraries are used? because we can simply use them within our program several times without having to build the function from scratch.

with the strlen function we can know the length of a text string. It is effective in large projects “reusing functions”.

In C we have two flavors!

two types of flavors static and dynamic libraries, both of which perform the same step when compiled.

  1. Preprocessing: Comments are removed and macros are detected
  2. The content of a: .c file is translated to .asm

3. The content of a: .asm file is translated to .o

4. Necessary libraries are linked to create the executable.

Link “Static”:

The functions are copied and placed in the additional executable it crashes and no library can be changed (function), the only way to change it is by overwriting the program that is “modify and recompile”.

Link “Dynamic”

The functions are not copied into the program but they remain available, and you use them when you need them, because you can change the functions without having to “recompile”

Creating dynamic Libraries:

  1. gcc *.c -c -fpic

All files with Extension .c are compiled in the current directory in an independent code, this means that they do not have a specific location linked in memory.

fpic is the one that makes the compilation position independent -c stops compilation and generates the .O files, which comes from a code independent of the position.

2. gcc *.o -sharing -o libholberton.so.

It takes all the .o files and converts them into a dynamic library

3. export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

The location of the library is defined with the environment variable.

--

--