Differences between static and dynamic libraries.

Mauricio Sierra Cifuentes
4 min readDec 16, 2020

--

Static Libreries:

Static linking is the process of copying all library modules used in the program into the final executable image. This is performed by the linker and it is done as the last step of the compilation process. The linker combines library routines with the program code in order to resolve external references, and to generate an executable image suitable for loading into memory. When the program is loaded, the operating system places into memory a single file that contains the executable code and data. This statically linked file includes both the calling program and the called program.

Static linking is performed by programs called linkers as the last step in compiling a program. Linkers are also called link editors.

In static linking if any of the external programs has changed then they have to be recompiled and re-linked again else the changes won’t reflect in existing executable file.

Statically linked program takes constant load time every time it is loaded into the memory for execution.

Programs that use statically-linked libraries are usually faster than those that use shared libraries

Dynamic Libreries:

In dynamic linking the names of the external libraries (shared libraries) are placed in the final executable file while the actual linking takes place at run time when both executable file and libraries are placed in the memory. Dynamic linking lets several programs use a single copy of an executable module.

Dynamic linking is performed at run time by the operating system.

In dynamic linking only one copy of shared library is kept in memory. This significantly reduces the size of executable programs, thereby saving memory and disk space.

In dynamic linking this is not the case and individual shared modules can be updated and recompiled. This is one of the greatest advantages dynamic linking offers.

In dynamic linking load time might be reduced if the shared library code is already present in memory.

Programs that use shared libraries are usually slower than those that use statically-linked libraries.

Some functions contained in the standard C libraries

How we create our own library

Method 1: Use libraries in the same directory #include “libreria.h”

Let’s take the example that I want to create a library for arithmetic operations and I want to include this library to test it in another file. For this we follow the following steps.

1. Create the header file

We create a file with the extension “.h” in the same directory as the main code, this file must have all the function prototypes and data type definitions from your library.

#ifndef _ LIBRARY

#define _LIBRERIA

float suma(float A,float B);

float resta(float A,float B);

float multiplicacion(float A,float B);

#include ″ libreria.c ″

#endif

2. Create the library code file

The library code file contains the header file that we created earlier and also contains the code of all the functions that were written in the header file.

#include ″ library.h ″

float suma(float A,float B){

return A+B;

}

float resta(float A,float B){

return A-B;

}

float multiplicacion(float A,float B){

return A*B;

}

3. Call the bookstore

When both files are finished, our library is ready to be used. We create the file test.c within the same directory, we include the standard libraries and the new library that we created.

#include ″ library.h ″

void main () {

int A = 1;

int B = 2;

printf ( ″ Result of% d +% d =% f \ n ″, A, B, sum ( A, B )) ;

printf ( ″ Result of% d-% d =% f \ n ″, A, B, subtract ( A, B )) ;

printf ( ″ Result of% d *% d =% f \ n ″, A, B, multiplication ( A, B )) ;

return 0;

}

Method 2: Use libraries in the compiler’s include directory #include <library.h>

This second method consists of saving a copy of the library that we created before, directly in the compiler folder. This will allow calling the library from any code as if it were a standard library.

1. Copy the library to the compiler folder

Copy the files “libreria.h” and “libreria.c” to the compiler’s include folder, the path of this folder may change depending on the program you are using. In my case I use the Dev-C ++ IDE and the path would be the following.

C:\Program Files (x86)\Dev-Cpp\MinGW64\include

2. Call the bookstore

After copying the files to that folder, the library can be used as if it were a standard library, without having to have the files in the main code directory. To call the library we use the instruction #include <library.h>, we use <> instead of »» because in this way the compiler will look for the library in the inclulde folder and not in the main code directory.

#include <libreria.h>

void main () {

int A = 1;

int B = 2;

printf ( ″ Result of% d +% d =% f \ n ″, A, B, sum ( A, B )) ;

printf ( ″ Result of% d-% d =% f \ n ″, A, B, subtract ( A, B )) ;

printf ( ″ Result of% d *% d =% f \ n ″, A, B, multiplication ( A, B )) ;

return 0;

}

--

--