Everything about dynamic libraries.

Juan Pablo Montoya
3 min readDec 15, 2020

--

In this article, I am going to talk about why using dynamic libraries, how they work, how to create and use one, what are the differences between a static library, and what are the advantages and drawbacks of each of them.

Why use dynamic libraries.

To get a better view on why using them let’s see the compilation process first, it’s preprocessing — compilation — assembly — and linking, we’re going to work on that last step, linking, that’s where a library gets connected to the executable file.

From my previous article we know how static libraries work, simply said when you use a function from a static library and you compile your program, the object code (or binary code) of your file and the object code of the function (the one you used in your file) come together to create your executable, this doesn’t seem so bad, but the drawback from this is that depending on how many functions and libraries you import your output or executable file is going to get bigger and bigger.

So dynamic libraries come in handy here they don’t work this way, instead of taking the entire object (binary) code of the function from the library, it gets its address in memory where if it’s not already loaded it will load it just once, and save a lot of memory and compilation time.

How to create one (Linux).

To create a dynamic library we will need the program gcc (GNU compiler collection).

To start we need all the object (binary) files of the functions that we want to include in our library, we also need to make the object files position-independent (it doesn’t matter where they are in the system it will run) so we can use this command:

gcc -fPIC -c *.c

That command will compile every file in the current directory one step before the linking aka output file.

After this step is done we can create our library with the files previously created using this command:

gcc -shared -o libraryexample.o *.o

The command above will create a library called “libraryexample.o” (name yours however you want) and the “*.o” will take all the output files that you created before.

Like that, you have a dynamic library with all the functions you want!

What are the differences between a Dynamic and Static library? (advantages and disadvantages)

  • The main difference is size, while the static library has to take the whole function object code the dynamic library only takes the function’s address which affects the memory usage a lot.
  • The second difference is portability, in a static library when you take your executable file the libraries used in that file if updated can’t be updated if the file hasn’t been compiled again, but in the dynamic library if the library itself gets updated you don’t need to recompile the whole executable for it to work.
  • The third difference is compiling times, in a static library, you have to take the whole object code of the functions you imported which takes more time than just taking the address of the said function as the dynamic library does, which decreases compiling time a lot.

--

--

Juan Pablo Montoya
Juan Pablo Montoya

Written by Juan Pablo Montoya

Software Engineer student at Holberton School

Responses (1)