Differences between static and dynamic libraries.

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.

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.

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.

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.

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.

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.

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store