The statics libraries

Roberto Ribeiro
IT student life
Published in
4 min readOct 11, 2020

¿What are they?

When we started programming in C our first tasks were to code a program that uses functions from the standard library “#include <stdio.h>”. But, what is this .h file that we included at the beginning of our program and why is it necessary to include it to use certain functions?

Are libraries literally libraries?

The definition of “library” found in Wikipedia tells us that:

A library is a curated collection of similar information sources and resources, selected by experts and made available to a defined community for consultation or loan, often in a quiet environment conducive to study.

This is something we all know, right? And when we setting up a study or work project, we always have as a reference some material such us our bibliography.
In programming, libraries is the place to store our tools, separated from the original project, that can be access as needed.
As in any other field, we seek only the data thats relevant to the our project.
For example, if our studing history, we focus only in the resources we need. When we are talking about the “First War” information about biology is excluded. Are we in the same page?

Yes of course, but you didn’t explain what libraries are in programming.

Libraries are pieces of code that have functions and variables already defined, which can be used or consulted by programs in their execution.
Continuing with the parallelism, the libraries have books, and these books would be the functions and variables to be consulted.
Samming up, the term library is used to refer to a set of object modules that are the result of a compilation, which have the extension “.obj” or extension “.o” and are grouped into a single file that usually has the extensions .lib, .bpl .a, .dll and vary depending on the operating system you use.

But how do I work with them?

First let’s define that there are two types of libraries, static and dynamic.
In this opportunity I am going to explain in detailed the functioning of the static libraries, but let’s clarify the definition about each one of them.

Dynamic libraries: in few words, we can say they are files that contain object code built independently of their location. They are to be required and loaded at runtime by any program though, instead of having to be linked, previously at the time of compilation.

Static libraries: they are the first ones created when programming languages started to be developed, formerly they were one of a kind. A static library, is a file that has several object code files packaged and at the time of compiling will be copied into the executable file. Unlike the dynamic libraries, they will remain within the executable file.

Can you explain that procces?

hell yeah! (obviously I’m talking to myself, am I going crazy? xD..)

To use a library we must have a header, a file with extension .h that contains the ‘’prototypes’’ of the functions. A prototype shows the name of the function, its arguments and return value, but not the entire associated implementation. They are built through the ‘’preprocessing’’ stage of the compiler by reading the libraries declared in the code, those listed at the beginning with the term #include.
But the compiler must be able to locate them, thats way in ‘’gcc’’ of Linux, the -L argument is used to define the path, and -l for the library name without the ‘’lib’’ prefix and without the ‘’.a’’ suffix.

Mmm… lets see it

The first thing we must do is transform our functions into objects, for which we use gcc with the -c parameter.

Next step, is to create the static library using the program “ar”, its name is an acronym for “archiver”.

The ‘c’ flag tells ar to create the library if it doesn’t already exist.

The ‘r’ flag tells it to replace older object files in the library, with the new object files.

Using the -t parameter in “ar” we check if the library has been created and what is its content.

After the library is created it must be indexed using the command ranlib. This is important because later it will be used by the compiler to speed up the search for symbols within the library, and to make sure that the order of symbols does not slow down the compilation proccess.

Finally, to compile our main, we must specify to gcc which is the name of our library. For that we use the -L parameter followed by the name of the static library we have.

For any doubts, leave your comment below, bye!

Shared:

https://twitter.com/ribeiro_uy/status/1315424122181165056

https://www.linkedin.com/feed/update/urn:li:activity:6721190087494823936/

--

--