The differences between static and dynamic libraries

--

One of the tools that compilers supply us with are libraries. A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. Normally the library is indexed, so it is easy to find symbols (functions, variables and so on) in them. For this reason, linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds up linking. Unix systems (as well as most other modern systems) allow us to create and use two kinds of libraries — static libraries and shared (or dynamic) libraries. Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime. This last comment seems obvious, as we already know that object files are also used only during the linking phase, and are not required during runtime — only the program’s executable file is needed in order to run the program.

Why use libraries?

  • Structure and organization of the default code, avoiding having to perform an analysis on where to place the different application files (resources, controllers, views, models, etc.).
  • Code reuse. Avoid code duplication.
  • Agility and speed in development. Thanks to the reuse of code we achieve faster development, since we will not waste time developing new functionalities.
  • Lower cost in development. Finishing a project earlier means that dedication is less and the cost of a project decreases.
  • Good development practices with the use of patterns. They are based on design patterns, which indicate guidelines on how to solve a specific problem that has already occurred before.
  • Minimizing errors and making it easier to solve possible errors that you may have will always be better than developing it from scratch.
  • Ease of finding code that already covers functionalities of a particular development.
  • Facilitates collaboration with other developers.
  • It facilitates maintenance when the application needs to be updated or an evolutionary one needs to be carried out, taking less time and reducing the total cost.

How libraries work

Once the libraries are created, they can be linked with the file containing the main function, or entry point, with gcc. Then, the code of the functions used in the program will be linked into the executable program, and it will be ready to run!

How to create libraries in Linux

Static:

Dynamic:

The ‘-c’ option makes sure the compilation process stops before the linker, and creates the ‘my_file.o’ file. We use an additional flag for dynamic libraries: ‘-fPIC’ or ‘-fpic’. This makes the code in the library position independent. PIC is code that works no matter where it is placed in memory. Why does it matter? Because several programs can use the same shared library, the library will have different addresses in memory for each program. And we still want our programs to have access to the code wherever it is stored.

Creating the ‘mylib’ library with our object file, in the first case, we use the command ar to create a ‘.a’ file (a stands for archive), and in the second case we use gcc with flags ‘-shared’, and of course ‘-o’ because we compile from an object file, to create a ‘.so’ file (so is for shared object).

Static:

Dynamic:

How to use libraries in Linux

We have to use is to compile and create our program. Let’s say our entry point is in the ‘main.c’ file, and we want to name our program ‘test’. Here’s the command to compile the program with our library in both cases:

The ‘-L’ flag tells the compiler where it needs to look for the library, so in this case where the library is in the current working directory, we just use a dot. the ‘-lmylib’ tells the compiler to link the code in main.c with the code in the library my_lib. Finally, the ‘-o’ flag allows us to give a name to the executable, in this case it will be ‘test’.

This is where dynamic libraries differ from static. With a static library, we can just run the code now. But with a dynamic library, it won’t work. A useful tool to show this is ldd, a command that prints shared libraries dependencies.

Differences between the two types of libraries:

The main advantage that a dynamic library has over a static one is that it takes less space in memory, because a static library will link all the function definitions in the program, whereas a dynamic one will look in the code and see which functions are called, and link only these to the program.

Another advantage of the dynamic library over the static one is that if we modify the code in one of the functions it contains, we don’t need to recompile, we can just execute the program again! This is not possible with a static library, we would have to recompile it (see the flowchart at the top of this article). Plus, several programs can use the dynamic library.

The static library also has advantages over the dynamic library. One of them is that the program using it is faster at execution time. This is because we have linked the library before runtime, whereas dynamic libraries are actually linked at runtime. Another one is that all the code of the functions is in the executable file, so there is not problem of compatibility.

--

--