Static libraries.

Santiagopinzon
3 min readJul 7, 2020

Along our way in programming, we will be working with libraries and I will help you understand them and see things like why to use them, how they work, and how they are created.

what is a static library?

Libraries are projects with specific methods or functions that you can add to other projects and complement them using their specific methods for a given solution.

Okay, so what does this mean in a nutshell?
Libraries are pieces of code made by third parties. This makes it much easier for us to program and makes our program easier to make and then understand.

A static library should always start in the name with lib and end with the extension .a

Why use libraries?

suppose that within a program you want to use a routine whose code has been previously developed perhaps by someone
This requires these three things:

  • know the parameters required and the type of data the function returns.
  • file with prototype of the .h function.
  • intermediate function code file.

libraries are very useful in these cases and when working in a big team they are very necessary.

How do they work?

When you start in the world of programming you will know that you have to divide your problems into small parts.
functions are blocks of code divided into logical declarations to solve a specific task.
Once you have created a static library, you will want to use it. You can use a static library by invoking it as part of the compilation and linking process when creating an executable program. If you are using gcc to generate your executable, you can use the -l option to specify the library (see gcc man for reference)

how to create a static library?

first we are going to use the command ar that together with the parameters r and c would be something like this

ar -rc libname.a

but before doing this let’s remember that the static library is going to save us the object files so let’s look at some example object files

with gcc and the -c parameter we get the extension .o which is the object file with *.c selection all the files that end in this parameter

Now we’re going to create the library and we’re going to link the object files, it would look like this :

After creating the library we have to index it by entering the command ranlib
let’s see your syntax

ranlib creates an index of the contents of libname.a and stores the index of libname.a. This is useful for linking and objects calling each other.

How to use them ?

a small example of how to use them is when compiling them put the -L and -l parameters let’s see this:

Here I am compiling my func.c file, then I use the -L parameter to indicate that the libraries can be found in the given directory. now the call of the library, before writing the name of the library put -l next to it to indicate the name of the library and write it without lib and also the extension .a

I hope you’ve understood this since it’s a fundamental part of a programmer’s life.

--

--