Libraries In Programing Languages

Joel Ramirez
3 min readMay 4, 2021

Functions are blocks of code that perform specific tasks, roughly, we can divide their use in 2, the declaration and the call. The declaration seeks to create and define the function and how it works as indicated in the following examples.

C: <functions.h>             | PYTHON: <functions.py>
int sum(int a, int b); | def sum(a, b):
<functions.c> | return a + b
#include "functions.h" |
int sum(int a, int b) |
{ |
return(a + b); |
} |

The next step is the call to the function, it consists of using an existing function to perform a task, this call can be made in any section of the code as long as the file containing the requested function is imported.

C: <main.c>                 | PYTHON: <main.py>
#include "functions.h" |from functions import sum
int main() |print(sum(2, 2))
{ |
int r = sum(2, 2); |
printf("%i", r); |
return(0); |
} |

A library is a collection of functions that are used at the disposal of the programmer in order to facilitate the work. with its use you can avoid the rewriting of code making use of the call of existing functions to perform a task or in other words, not reinventing functions.

Libraries can be created and included in 2 ways and their way of creating them gives them names, static or dynamic. In the case of static libraries in C, these can be created compiling our c files in object files: with the command:

gcc -c functions.c -o functions.o

and finally use this command to create the static library:

ar -rc libfunctions.a functions.o

To make use of the static library you need to include the header of the library in the main program, compile and execute.

C: <main.c>            
#include "functions.h"
int main()
{
int r = sum(2, 2);
...
}
gcc main.c -L . -functions
./a.out

In the case of dynamic libraries in C, these can be created compiling our c files in object files: with the command:

gcc functions.c -c -fPIC

and finally use this command to create the dynamic library:

gcc functions.o -shared -o libfunctions.so

To make use of the dynamic library, it must be added to the environment variable LD_LIBRARY_PATH so that it can be used by any program, and to finish, compile and execute

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH . /libfunctions.so
gcc main.c
./a.out

Once the library is created we can use the following commands to view its content

nm functions.o // nm functions.so Also works
functions.o:
0000000000000000 T sum

The biggest difference between these bookstores is the way they work.
The static libraries are added to the main program during compilation, in the linking phase, it means that the program must be compiled together with all the libraries it requires for its correct operation. On the other hand, the dynamic library contains its functions compiled and stored in its own executable to later be linked by other programs at runtime.

The static libraries must be present during the compilation of the main program, this creates a strongly linked relationship between the main program and the library, since the final executable will have everything necessary to function correctly by itself, and this can be an advantage or disadvantage depending on the case, however to modify the library and see the changes reflected in the main program, a recompilation is necessary.

In the case of dynamic libraries, being an external executable to the main program, they must be present during the execution of the program for its correct operation, as it is not part of the compilation but of the execution, the modification of the library does not require a recompilation to see the changes, in addition, its use can be shared by other programs (that is why they are also known as shared libraries), this is not possible with a static library, since for this, each program should have the library included in its executable code

--

--