Static libraries in C

A guide into characteristics and why is better use a library and how to create a Static library

Miguel Pacheco
5 min readFeb 27, 2021

Welcome to this blog, in this case i am going to talk about Static libraries in C but first we need to know what is a library

Always we write code right, but sometimes we think that it’s too boring write all the functions that we require in every single file. Well, we have a solution for that. and that solution is… yep, libraries, libraries in C are files that contains all the functions, variables, etc. that the files requires

The libraries are compiled to the program file in the linking step of compilation, if you want to learn what is compilation check this blog

Process of compilation in C using a compiler

Why use libraries?

  • Write code has a lot of optimization, so when you are creating your program or application, while more code and functions that you can reuse is better for have a good, clean and efficient product (in this case a clean code). In order to reuse functions, libraries is the solution

How do they work?

Libraries can be default or created by user, Either way, they have to be declared in a header file and include on the top of the program

Example of a code that prints something
Example of a code

How we can see, the library is on top of the program. In this case the library is stdio.h (standard input output).

When compiling, the static libraries are include in the executable file in the linker phase in a single file. This means that the code for these functions especifically becomes available for the program to use.

Well, now that we know what is a library and why we need to use library, we need to know how to create a static library

How to create a static library?

All files listed in a terminal
all .c files

This is a big collection of functions that made different actions such as indentify if a letter is upper or lowercase, if a character is a digit, etc. Instead of write every single function we can zip them in a library that we can reference every time we want to use them. To do so, we have to compile these .c (but stop after the Assembler phase, using -c option on our gcc compiler). The output will be our .c files transformered in objects files (.o files)

gcc -c *.c where the -c flag indicates that the compiler need to stop after the assembler and *.c will compile all the files ending with .c extension. Now we can see that all the outputs of all .c files have an .o extension:

outputs of all .c files

Static libraries are created using the ar command (archiver). This command is used in Linux to create, modify and extract files from archives. An archive is a collection of file that have a similar structure from which every file can be extracted.

ar takes objects files (ending with .o extension), as many as we want to include in the library and mashes all together into a .a file (Our static library). For to do this we need to run the ar command with followin flags and options:

ar -rc nameoflibrary.a <filename>.o

Where:

  • The r flag is used for replaces older object files (.o extension), with the new object files, and the c flag tells ar to create the library if it doesn’t exist already.
  • nameoflibrary is the name of the library to be created with the functions inside.
  • <filename>.o is every name of every object file that i want to include in my library, if i want to add all object files in the library, instead of write every single name of every file we can write *.o . This indicates that all files ending in .o will be included in the library.

After an archive (ar) is created or modified, it’s recommended to index it, some archivers do this automatically, depending of the OS (operating system). But it’s recommended run the command.

The command used to create or update an index is called ranlib , and is used as follows:

ranlib nameoflibrary.a

Example of indexing using the command ranlib in a terminal
Example of indexing using the command ranlib in a terminal

So now that we have created our object files from our program files (.c) compressed in an library and indexed it, now we can use the library.

How to use a static library?

Now that we have the library created we wanted to include it.

To do so, just run the compilation command with the specific options and flags. For example:

Format: gcc my_program.c -L. -lnameoflibrary -o my_program

Example:gcc main.c -L -lholberton -o quote

Example of compilation using a static library in a terminal
Example in terminal of compilation using a static librar

Where:

  • gcc is the compilation command.
  • my_program.c is the program that we want to compile.
  • -L. this tells to the compiler to look in this directory for library files.
  • -l will be followed by the name of the library we created (without the “lib” and the “.a” extension, for example: -lnameoflibrary )
  • -o my_program indicates the file name of the output.

You can see the content of a library (the .o files that went into it) using nm . The nm command provides mor information about the symbols in our static library.

Usage: nm nameoflibrary.a

Example of the use of nm command in a terminal
Example of the use of nm command in a terminal

Well, that’s everything. Thank you so much for read this blog, see you coming soon with another blog.

Happy Learning :)

--

--

Miguel Pacheco

Student at Holberton School | Aspiring to be a Front-End Web developer