Daniel Celis Tobon
4 min readSep 17, 2019

Why using libraries in general and how do they work?

Libraries are certain types of files that we can import or include in our program. Libraries contain the object code of many programs that allow you to do common things, such as reading the keyboard, writing on the screen, handling numbers, doing math functions, etc.

By being able to include these libraries, which contain the definitions of different functionalities, we can save a lot of time, for example: imagine that every time we need to read a user input, we must create a function that does it (this would be something really complex and inefficient), being able to have the libraries, we can make use of a variety of functions that will facilitate our lives and increase the efficiency of our codes.

What are the differences between static and dynamic libraries?

A static library is loaded when the program is compiled. The necessary functions of that library are copied into your executable. If you take the executable from one computer to another, the program will still work, even if the library is not in the new computer, since the executable has its own copy. The problem is that the executable will be bigger, since it has a copy of the functions of the library.

A dynamic library is loaded at the time of program execution, as needed. The executable does NOT have a copy of the library’s functions and needs the library to function. If you take the executable to another computer, you must also take the library or make sure it is already there. The advantage is that the executable is usually smaller.

How to create them?

If you want to see what is a static library and how to create it, go to this link

First we must find the files of the functions that we want to store inside the library. For our example we will use a script that finds the required files. We execute the script in the root directory (holbertonschool-low_level_programming) and the files that the script finds will be copied in the directory 0x18-dynamic_libraries.

The script to execute is the following:

#!/bin/bash
cp {$(find . -name "_putchar.c"),\
$(find . -name "4-isalpha.c"),\
$(find . -name "3-islower.c"),\
$(find . -name "6-abs.c"),\
$(find . -name "0-isupper.c"),\
$(find . -name "1-isdigit.c"),\
$(find . -name "2-strlen.c"),\
$(find . -name "3-puts.c"),\
$(find . -name "9-strcpy.c"),\
$(find . -name "100-atoi.c"),\
$(find . -name "0-strcat.c"),\
$(find . -name "1-strncat.c"),\
$(find . -name "2-strncpy.c"),\
$(find . -name "3-strcmp.c"),\
$(find . -name "0-memset.c"),\
$(find . -name "1-memcpy.c"),\
$(find . -name "2-strchr.c"),\
$(find . -name "3-strspn.c"),\
$(find . -name "4-strpbrk.c"),\
$(find . -name "5-strstr.c"),\
} 0x18-dynamic_libraries/

Once we have the files with the extension .c we proceed to compile to obtain the object files with the following code:

gcc -fPIC -c *.c

gcc (GNU Compiler Collection) is the standard compiler for UNIX-derived operating systems

-fPIC this flag generates position independent code (PIC) for shared libraries

-c compiles unlinked source files. This flag generates an object file (.o extension)

*.c selects all files with .c extension from the current directory

Now that we have the object files (.o) we proceed to create the library using the following code:

gcc -shared -Wl,-soname, libholberton.so -o libholberton.so *.o

-shared generates a shared object file for the shared library

-Wl pass option as an option to the linker

*.o selects all files with .o extension from the current directory

If we want to see the content of the object files we use the following command:

$ nm -D libholberton.so0000000000000a90 T _abs
0000000000000aa9 T _atoi
0000000000202048 B __bss_start
w __cxa_finalize
0000000000202048 D _edata
0000000000202050 B _end
00000000000011f8 T _fini
w __gmon_start__
0000000000000900 T _init
0000000000000bd7 T _isalpha
0000000000000c04 T _isdigit
0000000000000c25 T _islower
0000000000000c46 T _isupper
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
0000000000000c67 T _memcpy
0000000000000caa T _memset
0000000000000ce9 T _putchar
0000000000000d0e T _puts
0000000000000d4a T _strcat
0000000000000dcf T _strchr
0000000000000e21 T _strcmp
0000000000000e89 T _strcpy
0000000000000eeb T _strlen
0000000000000f15 T _strncat
0000000000000fa5 T _strncpy
0000000000001029 T _strpbrk
000000000000109d T _strspn
0000000000001176 T _strstr
U write