Dynamic Libraries using Linux and C

Samir Millan
4 min readDec 17, 2019

--

Why using libraries in general…

In C , certain types of files that we can import or include in our program are known as libraries (or libraries). These files contain the specifications of different functionalities and constructions and usable that we can add to our program, for example, read the keyboard or display something on the screen among many others.

Being able to include these libraries with limitations of different functionalities we can save a lot of things, imagine for example that every time we need to read by keyboard, we must then create a function that clicks (something really complex), to have the libraries in C , we can make use of a wide variety of functions that will facilitate our lives and increase the modularity of our codes.

Libraries are not external archive files created by others, it is also possible to create our own libraries and use them in our programs. The libraries can have several different extensions, the most common are: .lib, .bpl, .a, .dll, .h and some more not so common.

In conclusion: The libraries are files (not always external) that allow us to carry out different tasks without worrying about how they are done but simply to understand how to use them. The libraries in C allow to make our programs more modular and reusable, making it easier to create programs with quite complex functionalities in a few lines of code.

Static library vs dynamic library…

A static library is a library that “copies” into our program when we compile it. Once we have the executable of our program, the library is useless (that is, it is useful for other future projects). We could eliminate it and our program would continue to work, since it has a copy of everything you need. 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.

A dynamic library is NOT copied into our program when compiling. When we have our executable and we are running it, every time the code needs something from the library, it will search for it. If we delete the library, our program will give an error that cannot be found.

Creating a dynamic library…

When creating a dynamic object, it is necessary that said object code be independent of the position, to obtain this type of code you must specify the -fPIC (position independent code) option to the compiler. This flag should be indicated both in the compilation and in the library assembly. To mount the objects it is also necessary to indicate the shared option so that the result is an object file object that can be shared “.

Sintaxys…

Example#1
gcc -shared -fPIC -o libholberton.so fich1.o fich2.o
Example#2
gcc -shared *.o -o libholberton.so

EXAMPLE…

A make file like this can be used to compile the dynamic library:

#!/bin/bash
cat list|cut -d "." -f1|
while read CD_KND
do
Filename=$(find ../ -name "${CD_KND}.c" | head -n1)
echo "creating ${CD_KND} file"
gcc -fPIC -c "${Filename}" -o "${CD_KND}.o"
gcc -shared *.o -o libholberton.so
done

Finally you just have to run the file. You can call it what you want.

Advantages and drawbacks…

A program compiled with static libraries is larger, since everything you need is copied.
A program compiled with static libraries can be carried out on another computer without the need to carry the libraries.
A program compiled with static libraries is, in principle, faster in execution. When you call a library function, it has in its 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 do not affect them. If we change a dynamic, the executables are affected. This is an advantage if we have changed the library to correct an error (it is automatically corrected in all executables), but it is an inconvenience if you touch that makes us change the executables (for example, we have added one more parameter to a function of the library , the executables and facts stop working).

--

--

Samir Millan
Samir Millan

No responses yet