What is the difference between static and dynamic libraries?

Marcela Sanchez Moreno
5 min readDec 17, 2019

--

In this blog we will talk about the differences between static and dynamic libraries, how they work, why they are used, how they are created and used, their advantages and disadvantages. Now let’s go a little deeper into this…

Let’s start from the deepest:

What’s a library?

It is a compilation of the prototypes of functions that we use in our programs, the functions in programming language C, are declared and stored in a library, to be used at the time we need them, without the need to be copying the code every time this happens. This saves a lot of time when executing a program according to its complexity, accelerating the compilation process, additionally when working in teams this allows easiness in the tasks that are developed, easy reading and better management of the code, here the importance of libraries is evident.

A library in programming language C can be of two types:

  • Shared or dynamic library
  • Static Library

How do libraries work?

Static libraries, unlike dynamics, gather object files into one, while dynamics exist as separate files outside the executable, which means that static libraries add those files as they link, before it becomes an executable binary and dynamic libraries also have object files but are not added during compilation, they are kept separate from the executable binary, they are added to memory so that during execution these object files we need are kept available (linking process, final step of compilation).
The static libraries do not change while until we edit the functions contained in it, the dynamic libraries can at the time of compilation add functions during execution and this will not affect its operation, as previously mentioned, there is no need to compile again and again the program with the dynamic libraries.

In this blog we will explain step by step how dynamic libraries work, in my article “What about static libraries in C?” there is more information about static libraries that can be used to complement the current blog.

How to create and use libraries?

To create static or dynamic libraries, we start from the same point, we have to compile all our “.c” files this way:

gcc -c * .c

For the dynamic libraries, we add the flag: “-fPIC”:

gcc -c -fpic * .c

With the previous steps we will create the object files for both static and dynamic libraries.
The “-fPIC” flag that we add to the compilation for the creation of dynamic libraries gives the compiler the command that the object code is independent of the position.

The next step is the creation of the library.

For now we will focus on dynamic libraries, for more information on static libraries, read my blog “What about static libraries in C?”.

To create the dynamic library we must use the following command:

gcc -shared -o name_library.so file_object.o

The “-shared” flag gives the command to the compiler to convert the “.o” files into a dynamic library, by doing the above our object file can be linked to other objects to form an executable.
The “-o” flag places the output in a file.
If we follow the previous steps well, then we should have created a file with the extension “.so”, which means shared object.

How to use them?

One difference between dynamic and static libraries is that the former refer to the library that is placed in your program and the latter, on the contrary, copy the entire library.
Here we will link our functions in the code once the dynamic library is created:

gcc -Wall -pedantic -Werror -Wextra -L. main.c  l’name_library‘ -o

The “-L” flag commands the compiler to take the function it needs from the “.c” file, then places the library name with the “-l” indicator without the “.so” extension, at the time of linking it is not important to point out these endings because the compiler assumes it automatically.

Then we need to verify that the library is installed in its standard location (/ lib: usr / lib: usr / local / lib). To do this, we use the environment variable LD_LIBRARY_PATH. In this variable we search first in a group of directories where we could find the shared libraries:

  • / lib
  • / usr / lib
  • usr / local / lib

Then we add the route of the library with the following command:

export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH

At the end, if you want to verify that the above process has been done well, you can use the “nm” command (this command lists symbols of the object files) along with the name of your library and you will see a “list” of the functions within the library.

And finally this is the whole rough process that is used to create and use a dynamic library.

Additionally during reading you will find important and marked differences between dynamic and static libraries.

What are the advantages and drawbacks of each of them?

  1. Advantages:
  • Static libraries:

- Library previously loaded in an executable file.
- Not required at runtime or included when running.
- Faster compiling time and speed than linking to individual files and increasing binary code size.
- As the object code is included in the executable, multiple function calls are handled.

  • Dynamic libraries:

-The programs that use them don’t need to be compiled in case something in the code is edited.
-At runtime, a copy of the files is created so they are outside the executable file.
-Memory saving when running multiple library files.
-It doesn’t need to be compiled over and over again in case you have any editions after this process.

2. Drawbacks:

  • Static libraries:

- Each time you want to add more functions to the library, it must be compiled.
- When you have a copy of the executable, it may become too large a file.

  • Dynamic libraries:

- Long loading times and performance in terms of execution and binding
- Compatibility problems, if you change the library, the program that uses that library may not work and adjustments may need to be made.

--

--

Marcela Sanchez Moreno

Software Engineering Student 💻 Enthusiastic of macramé and crochet 🧶 ❤