Differences between static and dynamic libraries

In a previous article, we talked about static libraries in C and how they work. In this article, we are going to introduce the concept of shared, or dynamic libraries, the difference between a dynamic library and a static library, we will also explain the pros and cons of both types of libraries.

The examples that will be presented are made in the Linux operating system.

Why using libraries in general?

We know that a function is a named section of a program that performs a specific task.

When we are programming we realize that we need to use code that we have already done previously, for example, we can have several programs where we need to use mathematical functions created by us and we realize that we are repeating the code again and again, which possibly already we have in another program.

It would be great to be able to put those functions in a separate directory of the specific programs and have them already compiled, so that we can use them whenever we want.

The way to do this is to make libraries.Libraries are made of object files, with the extension ‘.o’, and can be made on Linux with the gcc compiler. Then, it’s up to us if we want to create a static or dynamic library.

How libraries work?

Static libraries:

A static library is a collection of object files that are merged by the linker with another object file to form a final executable file. So the role of the linker is to copy the code of the static library to the object file (the main program for exemple). This type of library is called “static” because you can’t modify it without recompiling it.

Dynamic libraries:

Dynamic libraries are also collection of object files, but unlike static libraries, they are outside the executable file. In fact, dynamic libraries are added to memory so the object files of the functions are available all the time. After the compilation process, during runtime, the program can just call the function it needs because it knows its place in the memory so it can load it.

How to create a library?

How to create them ? (Linux only)

For both (static and dynamic), the first thing to do is to compile all the source codes (*.c) into object code (*.o):

  • Static:
$ gcc -c *.c
  • Dynamic:
$ gcc -c -fPIC *.c

(The “-fPIC” flag tells the compiler the object code is independant of the position)

The objects files has been created

Now we have our object codes, we will create the libraries.

  • To create a static library called libschool.a, run the command: (note that a static lib. always begin with “lib” and end with “.a”)
$ ar -rc libmy.a *.o

It will archive (command “ar”) all of our object files in our library. The -r flag tells to replace the old object files in the library with the new one and the -c flag tells ar to create the library if it doesn’t already exist. To index it, run:

$ ranlib libmy.a

Your static library now exists and is indexed.

  • To create a dynamic (or shared) library called libholberton.so, run in your terminal: (note that, like static lib., dynamic lib. begin with “lib” but end with “.so” which stands for “shared object”)
$ gcc -shared -o libdynamic.so *.o

The “-o” places the output in libholberton.so, and the “-shared” flag is here to tell the compiler to convert the “.o” file into a shared library.

We’ve created a static and a dynamic library so we will now see how to use them.

How to use them ? (Linux only)

To use your libraries, no matter the type, you have to create and compile your program. We will take as exemple this function main.c:

The function _puts is in my libraries (static and dynamic)

If you try to compile main.c with the command gcc main.c, it will fail:

To compile it correctly, run this command: (remember that it works for both library type)

gcc main.c -L. -lmy -o puts
“-L” specifies the path to the library and the “.” refers to the current directory. “-l” specifies the library’s name without “lib” and without the suffix “.a”.

If “holberton” is a static library, the program will work but if it’s a dynamic library, it’s a bit different…

Indeed, when we try to execute the program, we get an error message:

To fix this, run in your terminal:

$ ldd puts

You will get:

libdynamic.so => not found

This message means that the library can’t be load at runtime so we need to update the environment variable LD_LIBRARY_PATH. This can be done by running:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH 
libdynamic.so => ./libdynamic.so (0x00007f407f76e000)

Another thing we can do is to check the symbols of our dynamic library. To do this, we can use the command “nm” with the “-D” flag:

$ nm -D libdynamic.so

And before executing, we can run a last command: “ldconfig”. This command will add our dynamic library to a cache which is searched when a program looks for a dynamic library:

$ ldconfig /usr/local/lib

You can now try to execute the program:

Our dynamic library has been created and we can use it !

Now let’s see the advantages and drawbacks of these libraries.

Advantages and drawbacks of static and dynamic libraries

Static:

Advantages:

  • At execution time, the program using a static library is faster because the library has been loaded in the executable.
  • The static library is not required at runtime.
  • Multiple function calls are handled because the object code of these functions is included in the executable file.

Drawbacks:

  • You must recompile your program when you modify your library.
  • Your programs including static libraries take a lot of spacein the memory.

Dynamic:

Advantages:

  • Unlike statics, you don’t need to recompile your program when you edit a dynamic library.
  • The library isn’t included in the final executable so it didn’t take a lot of space in the memory. Only the adress of the library is written in the program.

Drawbacks:

  • The execution and the loading time is long.
  • You can meet problem of compatibility when updating a dynamic library.

Thanks for reading and keep up the fun ! ;

--

--

--

Software engineer, Perú

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Sugar Advanced Workflow: All You Need To Know | Rolustech

Setting up a new Fauna Cluster using Docker

How to Study for the DevOps Leader (DOL) Certification Exam

Time left strings with Carbon and Laravel string helper

PWA : See Your Dream Come True!

Humanizing DevEverythingOps: What We Learned from the DevOps Unbound Roundtable on AppSec &…

File Handling in PYTHON

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christian Varas

Christian Varas

Software engineer, Perú

More from Medium

Introduction to Git and GitHub

Python essentials #2: How and why you should use the OOP (also let’s talk about big O and…

Introduction to Version Controlling

Multithreading in Python