Static Libraries vs. Dynamic Libraries in C

In this publication we mainly explain the process of creation of Static and Dynamic libraries, how to use them and the advantages and disadvantages of each one.

What is a Library in C?

Libraries are known as certain types of files that we can import or include in our program. These files contain the specifications of different functionalities already built and usable that we can add to our program, such as reading from the keyboard or showing something on the screen, among many others.

How libraries work?

Once we have created the library, it can be linked to the file that contains the main function, or entry point, with gcc. Then the code of the functions used in the program will bind to the executable program and it will be ready to run.

What is a Static library?

A static library is a library that is “copied” into our program when we compile it. Once we have the executable of our program, the library is useless (in other words, it is used for other future projects). We could delete it and our program would continue working, since it has a copy of everything it needs. Only that part of the library that is needed is copied. For example, if the library has two functions and our program only calls one, only that function is copied.

How to create a Static library?

In order to put our code in a library, we need to organize it as follows:

How to use the Static library?

We will use an example to explain this.

#include "holberton.h"int main(void)
{
_puts("Programming is my passion - Lex");
return (0);
}

What is a Dynamic library?

A Dynamic or Shared Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory.

How to create a Dynamic library?

In order to put our code in a library, we need to organize it as follows:

How to use the Dynamic library?

Since Dynamic Library is linked during runtime, we need to make this file available during runtime. The dynamic linker searches standard paths available in the LD_LIBRARY_PATH and also searches in system cache for dynamic libraries. So our next step is to add our working directory to the LD_LIBRARY_PATH environment variable so that the linker can find our library file. The below command is used for this.

Differences between static and dynamic libraries

  1. Linking time

Advantages and Disadvantages of Dynamic Libraries

  1. It only needs one copy at runtime. It is dependent on the application and the library being closely available to each other.
  2. Multiple running applications use the same library without the need of each file having its own copy.
  3. However, what if the dynamic library becomes corrupt? The executable file may not work because it lives outside of the executable and is vulnerable to breaking.
  4. They hold smaller files.
  5. Dynamic libraries are linked at run-time. It does not require recompilation and relinking when the programmer makes a change.

Advantages and Disadvantages of Static Libraries

  1. Static libraries resist vulnerability because it lives inside the executable file.
  2. The speed at run-time occurs faster because its object code (binary) is in the executable file. Thus, calls made to the functions get executed quicker. Remember, the dynamic library lives outside of the executable, so calls would be made from the outside of the executable.
  3. Changes made to the files and program require relinking and recompilation.
  4. File size is much larger.

Bachelor Industrial Engineering