Dynamic libraries

Dynamic libraries

What is and why using libraries

A library is a set of functions and variables put together in such a way that by calling that library, its variables and functions can be accessed without having to define them. And this is exactly the main reason why to use them. We can access global or own functions without the need to define them in each file that we are going to use for our code. In this way, we can use, for example, the "printf" function simply by calling the library that contains it "stdio.h" instead of having to develop its functionality in our file. In this way we save a lot of code and improve the efficiency of our program.

How do they work

Every library consists of two parts: a header file and the code file. The header file, normally denoted by a ".h", contains information about the library and contains constants and types, along with prototypes for functions available in the library. There are two types of libraries: Static and Dynamic. Let's see how each of them works.

Static libraries: These types of libraries are static because they are copied to our code at the moment of compilation (in the last phase: linked) and become part of it. Once it is inserted it is historical like the rest of the code. That is, no matter how much the library changes after compilation, our code will not be modified, it will remain static with the versions of the functions that were in the library at the time of compiling. It is important to mention that only the parts of the library that our code calls throughout it are copied and not necessarily all the content of the library. This means that if we have a LIB library with two functions (a and b), and in our code we call the library and use the function b in it, in the compilation only the function b will be copied.

Dynamic libraries: On the other hand, dynamic libraries are not copied into our code when compiling. Only a pointer to the functions that are called in the code is recorded and when it is executed, they are accessed. This means that if the library is modified after the elaboration of our code, its performance can be modified since it is at the moment of execution that the information is sought. Consequently with this our same executable can have two different performances depending on whether there was a change in the called dynamic libraries.

We can see then that the main difference between these types of libraries is when our code accesses them and uses the information. In the static ones when compiling and in the dynamic ones every time the executable is run.

Not necessarily one type of library is better than the other, both types have their advantages and disadvantages:

  • File size: A program compiled with static libraries is larger, since everything it needs is copied.
  • File and library independence: A program compiled with static libraries can be transferred to another computer without the need to carry the libraries.
  • Speed of execution: A program compiled with static libraries is, in principle, faster in execution. When you call a library function, you have it in your code and you don't have to go read the dynamic library file to find the function and execute it.
  • If we change a static library, the executables are not affected. If we change a dynamic, the executables are affected. This can be an advantage or a disadvantage depending on the case. It is important to keep track of the changes in the library especially if we use dynamics since the behavior of our code could be modified.

How to create a library in Linux

A - Dymaic Libraries:

1- Put all .c files together in a folder

2- Generate for each c file its object file

3-Put all all objects files into one library

Lets see with an example 👀        

Files: 2-funciones.c , 3-funciones.c , 4-funciones.c, 5-funciones.c

As we can see below, each file contains a group of functions inside:

No hay texto alternativo para esta imagen


We want to make a Library call libdynamic.so with all this functions inside.

First step: Put all the ".c" files together in a folder.

No hay texto alternativo para esta imagen

Generate for each c file its object file: For that use gcc with the option c and with fPIC for generate the position independent code.

gcc -c -fPIC *.c        
No hay texto alternativo para esta imagen

Once you have the object files, they must all be put together in the library. In our case libdynamic.so For this we use also the gcc but with shared.

gcc -shared -o libdynamic.so *.o        

Congratulations, Your library was created!

To call your library you just need tu put "nm -D" and the name of the library.

nm -D libdynamic.so ✔        
No hay texto alternativo para esta imagen

Once you have created your library to use it, the first thing you should do is set the environment variable LD_LIBRARY_PATH. This way the system would know the location of yours files.

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH        

To use them in Linux the only thing you have to do is compile your mains with de opction -L and lydinamic.

gcc -L. main.c -ldynamic        

B- Static libraries

To create a static library the process is a little similar

1- Put all .c files together in a folder

2- Generate for each c file its object file: CODE gcc -c *.c

3- With the "ar" put all objetc files togethers in a stat lib: CODE ar -rc lib.a *.c

To view or add a comment, sign in

More articles by Marcela C.

  • Science, Technology , Engineering and Maths & VR/AR

    What does “STEM” mean? The term STEM is an acronym that comes from English and its initials refer to each subject that…

  • what happens when you type https://www.google.com?

    Have you ever wondered what happens behind the scenes when you click on google.com? If you have done them you probably…

  • IoT : Internet of things

    Internet of things refers to the idea that "things" can have internet and communicate with other things or people. We…

    1 Comment
  • Recursion( recursion(recursion) )

    What is recursion? It is a process that defines itself in terms of itself or in other words it is a function that calls…

  • Mutable, Immutable in Python

    In Python, everything is an object. Numbers, strings, lists, they're all objects.

  • Attributes: Classes and Instances (PY)

    Class A class serves as the primary means for abstraction in object-oriented programing. As we know Python is a OOP and…

    1 Comment
  • Blockchain: Primer acercamiento

    Blockchain es una tecnología que se utiliza para almacenar información. Sí solo nos basamos en esta definición…

    9 Comments
  • Step by step " ls -l * .c " in a shell

    In this article we are going to go through the step by step behind executing "ls -l * .c" in a shell.

  • C static libraries. Why use it and How!

    In this article I'm going to go over why use C libraries when you are writing a program, how they work and how you can…

  • Has the time of the socially responsible investor arrived?

    Responsibility is a concept that started to be introduced in our society over more than a century ago. Initially, it…

Others also viewed

Explore content categories