C static libraries
Juan Sebastian Gonzalez
Software Developer | Full-Stack Developer | Javascript | React | Nextjs | Redux | Node.js | Python | Django | DRF | Nestjs | Fast Api | Docker | MYSQL | MONGO | ARANGODB
This article is about exploring in C what are the static libraries, why should we use them, how they work, how can be created, and finally how to use them.
Before starting, take a brief look at the compilation phases in c.
Libraries are used in the linking phase, which means that at the end are expected a set of ".o" files to be linked on.
What are the libraries in C?
A library is a file that contains several object files and can be used in the linking phase of a program. What we could expect to find when these are used is that the library file is indexed. This makes it easy to find symbols such as functions, variables, and so on, inside of them. which means that the libraries themselves are a collection of code routines that can be called at the moment of the building stage of our programs.
Unix systems allow us to create two kinds of libraries. Static and dynamic (shared).
Why should we use libraries?
There are basically two reasons why is recommended to use libraries.
What is a static library and how does it work?
It is a file containing a collection of object files (*.o). Files that are linked into the program in the linking phase (4) and not are important in the runtime.
The linker, in this case, copy the code of the library to our object file. So the library becomes a collection of object files merged by the linker. These libraries are normally recognized because have the extensions ".a" or ".lib".
How to create static libraries in c?
Just suppose that we have a set of ".c" files in our directory like in the image above. what the linker does expect is to get the ".o" files, then we must make them.
To achieve this, we must use gcc -c, and for speed up use *.c which means a match with all the files ending with ".c" extension in the current dir, otherwise we have to add the files one by one or at least copy in this way.
2. bundle all object files into one static library.
To create a static library or to add additional object files to an existing static library, we have to use the GNU?ar?(archiver) program.
领英推荐
$ar -rc libr.a *.o
In this way, we create a static library named "libr.a" and puts copies of the object files ".o" created before in the current dir into the file.
3. After an archive is created or modified, there is a need to index it. and this can be complete by using the next commands
$ranlib libname.a
Or directly using a third flag "-s"
$ranlib -rcs libname.a *.o
Like we can see this creates the "libr.a" file already indexed.
How do I verify the library content?
In order to list the names of the object files in our library, we can use the ar command with?-t?flag.
ar -t libr.a
How to use the static library?
4. Once we have the library file created, the final step is to create the final executable program.
$gcc main.c -L. -lname -o main
-L: Specifies the path to the given libraries ('.' referring to the current directory)
-l: Specifies the library name without the "lib" prefix and the ".a" suffix, because the linker attaches these parts back to the name of the library to create a name of a file to look for.
Next, we have an example of the right execution of the compilation including the library, in this case using a created function called _isdigit() which returns 1 if the input char is a number or 0 if not.
With this, we finish with an executable file, with all the functions listed before, by using just a couple of commands in the shell. Hopefully, you found this useful.