Static and Dynamic libraries

Diego Armando Lopez Quevedo
4 min readSep 16, 2019

--

Why using libraries in general ? A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. When we work in a programming language like C, static libraries or dinamic libraries are collections of object files that are linked into the program during the linking phase of compilation.

Libraries contain the object code of many programs that allow you to do common things, such as reading the keyboard, typing on the screen, handling numbers, performing mathematical functions, etc.

How do they work

If we want to use a program written in a programing language like C, its necesary to compile the program in code that the machines can understand. That code is a binary flow from 1 to 0, so it is necessary to use a compiler that converts the C code into machine code. To compile a C program it is necessary to use the GCC compiler.

Normally the compilation process is divided into 4 phases: the first step is the “Preprocessor”, that remove the comments, macros are expanded, and header files are replaced with the text contained inside them. Then the “Compiler” Take the file code “C” and create the assembly code. In the “Assemble”, the assemble code is convert into the object code. And finally the “Linker”, that combines the libraries and the code together to form into a single file an executable machine code (executable program).

In the Static library the linker takes the object code and generates an executable file in which the function codes are included, which the function codes are included, while in the Dynamic libraries the functions are loaded into memory so the linker no longer takes the object code with the function code but the object code with the memory address of those functions available in memory.

How to create them

First all, To create a static library in C, we have to compile our library code into an object file using -c with GCC Compiler. (example: if the file is main.c the result file will be main.o)

For example:

$ ls *.c 
file.c file2.c file3.c
$ gcc -c *.c
$ ls
file.c file.o file2.c file2.o file3.c file3.o

For Static libraries we use the “ar” program for “archiver” to create our archive files. In this case the command create a static library named ‘libholberton.a’ and puts copies of the object files “.o” in it. Also we can list the names of object files in the library using “-t” command.

$ ar -rc libholberton.a *.o

The command “ranlib” creates an index of the contents of libholberton.a and stores the index in libholberton.a. The index lists each symbol defined by a member of an archive that is a relocatable object file. This is useful for linking and in case the objects call each other. An archive with such an index speeds up linking to the library and allows routines in the library to call each other without regard to their placement in the archive.

$ ranlib libholberton.a

To list the symbols stored in the static library we can use the command “nm”, which lists each symbol’s symbol value, symbol type, and symbol name from object files.

$ nm libholberton.a

Otherwise, for Dynamic libraries start by compiling your “.c” files with the following command:

gcc -c fPIC *.c

-fPIC: Compiler directive to output position independent code, a characteristic required by shared libraries.

This command will take the “.c” files and return object code files ending in “.o”:

$ ls
file.c file2.c file3.c holberton.h
$ gcc -c fPIC *.c
$ ls
file.c file.o file2.c file2.o file3.c file3.o holberton.h

Now, to create the Dynamic library for these files we use the flag “shared”, which definition is “produce a shared object which can then be linked with other objects to form an executable”.

$ gcc -shared -o libholberton.so *.o

The Dynamic library is now created.

How to use them

It is now possible to use the static or dinamic library as part of the compilation and linking process when an executable program must be created, as in the following example, using the main.c file that prints a little quote.

$ gcc main.c -L -lholberton -o quote

As we see the compiler identifies the library (.so for dynamic, .a for static) so is not necessary to put libholberton.so or libholberton.a, just lholberton.

Finally, we add the environment variable to the library path, so the program knows where to look for library files.

$ export LD_LIBRARY_PATH=PWD:$LD_LIBRARY_PATH

And now we can run the executable program ‘quote’ to can see it.

$ ./quote
"At the end of the day, my goal was to be the best hacker"
- Kevin Mitnik

Some differences between Static and Dynamic libraries: Advantages and Disadvantages

If we compare static and dynamic libraries, the aspects to consider are basically size, speed and updates.

As we see, in Dynamic libraries the libraries are added to the memory, unlike Static libraries where the linker takes the object code and generates an executable file in which the function codes are included. This means that Static libraries are much bigger in size, because external programs are built in the executable file, but that makes the Static libraries faster in the execution because the functions are inside the executable, and we do not have to look for them.

Other thing is that in th Static library the executable file will have to be recompiled if any changes were applied. In Dinamic the library will be loaded in memory, so there is no need to recompile the executable.

--

--