¿Library in programming? let’s see static library in c

Alejandra Higuera
4 min readOct 15, 2020



A “library” is a collection of parts of the program that do common and / or specialized things that prevent the programmer from having to “reinvent the wheel” when writing software. It generally consists of functions to call and object classes that you can instantiate. then A library is one or more functions that we have already compiled and ready to be used in any program we make.

There are two libraries types in ways how work below you can see the differences between them

difference between libraries types

Advantages or Disadvantages

  • A program compiled with static libraries: it is larger, since everything it needs is copied, it can be taken to another computer without having to take the libraries, faster in execution because when it calls a function in the library it has it in your code and you don’t have to go read the dynamic library file to find the function and execute it.
  • If we change a static library, the executables are not affected. If we change a dynamic, the executables are affected. This is an advantage if we have changed the library to correct an error (it is corrected automatically in all executables), but it is inconvenient if touching that makes us change the executables

So when it comes to choosing It is as always a question of compromise between the advantages and the disadvantages. for simplicity we will continue to see how to use and create static libraries

Create a static library

  1. Prepare source .c files that currently contain our programs that execute a specific activity, as you can see in the example there are four files.c
Example files.c

2. We need to compile our library code into an object file, using : (visit gcc compiler)

 $ gcc -c *.c

As you can see all files with extension .c in the current working directory have been converted into their respective .o object files

files.c compile into file.o

3. Once we have the object files, we use the ar command to create our final library / file, where we will include the object files. *.o

Archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single file.

$ ar -rc libholberton.a *.o
$ ar -t libholberton.a // to vertify the content of our library //
  • rc option: This tells ar to create a file (option c) and insert the objects, replacing the oldest files where necessary (option r).
library view

We have now created a static library libholberton.a and now we are going to use the static library by invoking it as part of the compilation and linking process when creating an executable program. In case of gcc, we use the following flags to create a static library

  • l <library name without prefix and lib extension>
  • -L: specifies the path to the library. We can use -L. to point to the current directory and -L / home / tmp to point to the / home / tmp directory.
$ gcc main.c -L. -libholberton -o new

Now run the ‘main’ executable program.

Generate an index

The C compiler uses the index to perform a symbol search. The index speeds up this symbol search process.
To create, or update, said “index” of a sample file “libholberton.a”

$ ranlib libholberton.a

Whenever files are added to a library, including the initial creation of the library, the library must be indexed, which is done with the ranlib command. ranlib creates a header in the library with the symbols from the content of the object file. This helps the compiler to quickly reference symbols. A large library can have thousands of symbols, which means that an index can significantly speed up the search for references.