C static libraries
A static library is a compiled object file containing all symbols required by the main program to operate (functions, variables etc.) as opposed to having to pull in separate entities.
So we will have all the symbols in a pack, to use them when we need it.
Cool, isn´t it? Finally we can save time and efforts finding different functions, prototypes, variables and so!
Static libraries are better for quickly getting a library up and running, but it also has drawbacks. It gets compiled into every program that uses it.
This is so refreshing, but how do the static libraries work?
Let´s answer the question explaining the steps to create a static library.
1. The first step to create a static library is to gather all the files or functions that you want to include in the library. All of these files should end in “.c”.
2. When you get all "c" files in your directory, compile and convert them in ".o" files like this: gcc -c *.c
3. Cool, now our files are ready to archive in our library! let´s write this in our command line: ar rc YourLibraryNameHere.a *.o
4. Now we have created our library, greetings!
-The objetive of this work is to convert all .c files in .o files to use them into a library-
Now if you want to check if your work is succesful, just type ar -t <your library name> , you will see the files into your library with .o; now my friend you need to index this library to use without issues.
To make it just type ranlib <name of a. file>, with this we create a index of the file. The index lists each symbol defined by a member of an archive which is simply relocatable object file.
Cool, just funny! But how do i use those libraries?
Using a static library is really easy. All you have to do is compile the file using gcc and add the library name to the command.
Example:
gcc YourFileNameHere.c YourLibraryNameHere.a
Compiling your file with a library is much quicker and easier way to use specific functions.
Proudly written by Imanol Asolo (Code Codix) on 22/10/2021
Comentarios
Publicar un comentario