Static libraries!

Static libraries!

Why use libraries in the programming world?

A good way to understand why use libraries is when we are making a program and it becomes too long and some segments of the code become repetitive. Having a long code we have to take in mind the compiling process time and how to be more recursive to optimize, the best way to solve a problem is to divide it into smaller problems.

With a library we can make code segments that may be needed in many programs, like mathematics functions or read inputs of the keyboard, the libraries within contain variables and functions, which can be imported into a program that requires something from them.

How the libraries work?

The libraries are one of the tools of the compiler, these contain for the most part object code and can be used as a single entity in a linking phase of a program (read more here!). All libraries are ordered by index to make it easy to find symbols (Variables, functions and so on) within.

Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during the runtime.

In conclusion, linking a program whose object files are ordered in libraries is faster than linking a program whose object file are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds up linking.

How to create a static library?

Is "ar" (archiver) the basic tool to create a static library on Linux. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, and so on. The syntax looks like below:

No alt text provided for this image


The command creates a static library named "libexample.a" and put a copy within of the object file "example.o". The flag "r" replace older objects files in the library with the new objects, the "c" flag create the library if it doesn't already exist.

After an archive is created, or modified, there is a need to index. The index is used by the compiler to check the correctly order of the symbols in the library, so to index the archive we use:

No alt text provided for this image

Now that the file is indexed, you can check what object code contains, there are two ways, using ar with the flag "-t" or using the command "nm" see below:

with ar command:

No alt text provided for this image

with nm command:

No alt text provided for this image

NOTE: when an archive file's index generation date (stored inside the archive file) is older than the file's last modification date (stored in the file system), a compiler trying to use this library will complain its index is out of date, and abort. There are two ways to overcome the problem:

  1. Using "ranlib" to regenerate the index.
  2. When the file is copied, use "cp -p" to keep all attributes of the file, including its access permissions, owner (if "cp" is invoked by a superuser) and its last modification date.

How to use static libraries!

We will use a function to print a phrase from Kevin Mitnick on the terminal, the main code is:

No alt text provided for this image

As we can see, the way to print the quote is using a function _puts. So, inside of example.o is the function to print write in object code. Like a current function we need to have the prototypes of the functions in a .h file, in the example is "holberton.h" that contains the prototype of _puts.

The next step is compile, using this:

No alt text provided for this image

The compiler creates a program with the object code of example.o and the any symbols that the program need int the library example, in the compile step we don't need use the prefix "lib" and the extension ".a". The "-L." flag tells the linker that libraries might be found in the given directory.

Now just run the program to check:

No alt text provided for this image


To view or add a comment, sign in

More articles by Juan Uribe

  • what is IoT?

    In simple terms, The Internet of Things (IoT) refers to the constant tendency to connect all kinds of physical objects…

  • Machine learning!

    First of all the context, everyone talks about machine learning so we will do it too! This is a subfield of computing…

  • Python3: Mutable, Immutable... everything is object!

    Brief introduction! Object Oriented Programming (OOP or OOP) is a programming paradigm, Python is a language OOP—what…

  • Dynamic libraries!

    Why use libraries in the programming world? A good way to understand why use libraries is when we are making a program…

  • What happens when you type ls -l in the shell

    This post describes step by step how a command line interpreter works, when the command ls -l is typed in the command…

  • What happens when you type gcc main.c ?

    GCC is an integrated compiler of the GNU project for C, C++, Objective C and Fortran. This compiler is capable of…

  • What is the difference between a hard link and a symbolic link?

    Consider the following scenario: There is a file deeply buried in the file system called…

  • What happens when you type ls *.c

    The commands on linux are reserved words that the Operative System uses to execute determined actions using a terminal…

Insights from the community

Others also viewed

Explore topics