In this lines i will write about C libraries, first of all, i will answer the question: why using libraries in general?

As we are making programs we realized that some code parts are often used, and some functions are called in many programs around and we ask: what if we can include those functions in a separate directory, but not just have the code, what if you can save it compiled and you can use it every time you need it?. In that way, you don’t have to rewrite the code and waste some time compiling that code every time. That is why is useful to have libraries.

C language have dynamic and static libraries, in this blog we are going to explain a little bit of the first ones. How it works dynamic libraries in C programming? We have to have your .c files with the functions you are going to use and the header file (.h) where we have to place the functions prototypes as we can see below.

The lines containing the #ifndef — #endif are importan to avoid multiple definitions of the builtin libraries and functions. And yo can have one or more files containing the functions included in the header file. Now to you have that .c and .h files, you have to type the following command: This command will generate objectt files for each .c file that you have in the current directory. After that you have yo type: (note that the part “ball” is the name of the dynamic library).This code tells the computer to select all the object files (.o) and compile it creating a dynamic library with the -share flag instruction. If the process works well, you will have a .so file containing your dynamic library. Las but not least, you have to export the path of your library to permits other programs use that functions. For doing that, you have yo type the command: Thats it, now you have your library.

How can you use it on linux?

To compile your code use the following command line: gcc -L test.c -lball -o test. The -l with the world “ball” tells the compiler to searchmfor a dynamic library named ant the -L instruction will look for the lybrary in the current directory. In our header file example bellow and with the test.c, for instance:

#include "operations.h"int main(void)
sum(2 + 6);
return (sum);

With the command of the example we will get an executable file that will display the output: 8. Thats it, now you know how to create and use dynamic libraries.