C static libraries
Why use libraries
Generally the programmers are organized peoples, and if we have somethings in common is that we don't like to do many times the same things, that's s boring. We always find something that makes the boring task for us.
Imagine that, at some point, you created a function to add two numbers, then you created another to subtract, to multiply and divide, later you created another to manipulate strings and as time went you created many functions to do different things, Now imagine that you make a program which uses thirty functions that you have previously created, the compilation process is so complicated, since you have to name each of the functions you want to use, you can make a typing mistake or you can forget the name of some function. Well what if I tell you that you could forget to name all your functions when you are compiling, with only file, I mean, a single name to add in the compilation process and also organize your working directory, having all your functions in one place. And all this you could do if you knew something called libraries. Let me tell you a little more about this
How they work
The libraries are not more than a collection of functions, previously already compiled and prepared to be reused in any program that you want to do.
They are used very frequently to save work and, in addition, they are often shared among programmers to decrease development time.
A static library is a library that is "copied" into our program when we compile it. Once we have the executable of our program, The library is useless, I mean, it is useful for other future projects. We could erase it and our program would continue to work, since it has a copy of everything it needs. For example, if the library has two functions and our program only calls one, only that function is copied.
How to create them
In order to put our code in a library, the first thing we need to do is organize it in the following way
- One or more ".c" source files with the code of our functions.
- One or more header files .h with the types (typedefs, structs and enums) and prototypes of the functions that we want to use.
- One or more files resulting from the compilation process of our functions
What should go inside the header.h file?
Each prototype must correspond to each function that you want to use in the library, as shown below
What should go within each function with extension ".c"?
The corresponding code must go inside the function, since it will be executed when it is called in some instance, as shown in the following image.
Compiled functions (extension ".o")
When the compilation process of a function is carried out, a file with extension ".o" is generated for each function that is compiled. To compile a function, just use the command “gcc” with the “c” option, which indicates that only the function will be compiled and nothing else. as shown in the image below.
gcc -c “function name”
Once we have all our files ready to build the library, all that remains is to carry out a procedure using some GNU / Linux system tools, which are so simple to use.
How to create them
First of all we have to talk about "ar" command. This is a command normally used to create, modify and extract files from the archives. Keeping in mind that a archive is a collection of many files.
How every command in Linux, it has its options or flags to perform different task, in that case the option it will use the "r" option, but you wonder why these two options? Well calm friend, I'm going to say why use the option "r"
"r" is commonly used to create the file or in this case the library, this option inserts the files inside the archive, showing an error message if any of the entered names does not exist or is written badly
Example of how to use the "ar -c" function
ar -c “library name” “name compiled file ”
Where the library name is the name that the library will have and name compiled file is the name of each of the functions compiled previously. Now imagine that you have twenty compiled functions, that is, twenty files with the extension ".o", you would have to write the twenty names, but i am going to give you a trick, just use the wildcard "*o" and tadaa !, all the files inside your library.
Now you are wondering if the process was successful? Well, to check what's inside your library is very simple, you just have to write the following command.
ar -t “library name”
After an archive is created, or modified, there is a need to index it. Ranlib adds or updates the object files in a static library. Linkers can use static libraries when linking in order to provide the symbols that the code needs to function.
ranlib “library name”
How to use them
Using the libraries is something very simple to do, you could say the most complicated process is to create them. To use them you should only add the name of the library to the list of file names that is given to the linker, using the special flag "-l" here is an example
gcc main.c -L. -"library name"-o "name of exutable"