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?

  • 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?

how to create a static library?

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 ?

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.

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store