STATIC AND DYNAMIC LIBRARIES

Flavio Espinoza
3 min readDec 16, 2019

Why use libraries in general

Libraries offer us efficiency with the compilation of the program. It allows the operating system to only use one call to the library for the program being created instead of calling and compiling each individual file. This allows the stack to free up space and be able to before other tasks. While the same could be done by referencing each individual files and calling the header file and concurring code at the same time, it saves much more space and is generally executed in less time as well.

How do they work

They work by having a set of data which is called to during the linking process versus adding all the date into the executable during the linking process. They call function files whenever found inside of the driver code.

How to create them (Linux only)

In order to create a library, one has to go through a few steps:

The first being capturing all the files you want to part of the library.

Once you’ve figured out which files you want to add to your library, you need to convert them to from .c files to .o files for the library to be able to compile. This is done with the following command:

gcc -fPIC -c *.c

The fPIC flag generates position-independent code. Meaning, the object files will be stored in a relative position to their individual files rather than have absolute locations in memory.

Upon the compilation of the files, you need to compile the library. This is done by running the command:

gcc -fPIC -shared *.o -o liball.so

This command tell the terminal to run the gcc compiler and use the -shared, -fPIC, -o flags to generate the liball.so library. The -fPIC flag was previously talked about; the -o just specifies what the output will be named.

How to use them (Linux only)

They’re used/ called during the compilation process; the gcc statement to compile using the library would be something as follows:

gcc -L. -lname program

where -L. tells it that its looking for a library and -lname, tells the compiler which library its calling (-l is the flag and name is the name of the library without the lib part of the library name or the .so extension.

What are the differences between static and dynamic libraries

The main differences is the order in which linking occurs. The linker has preference over .so files than .a files. A static library would be referenced using the .a extension, which has less precedence. Another difference is in how flexible it is. The static library can however run program while also updating function files without any conflict, whereas the shared libraries require and update to the library file to be able to compile correctly

What are the advantages and drawbacks of each of them

The advantages are that the dynamic/ shared library will be referenced before the static library, as the linker draws precedence over the .so extensions rather than the .a extension.

It also offers a shorter run time, and takes up less space. This is because shared libraries contain calls to the functions instead of having them built in to the linking/ execute stage

Static linking offers you a more comprehensive process while using up more space and time.

--

--

Flavio Espinoza

A software engineering student @ Holberton School.