What is a Library in C?

Libraries are known as certain types of files that we can import or include in our program. These files contain the specifications of different functionalities already built and usable that we can add to our program, such as reading from the keyboard or showing something on the screen, among many others.

Why use libraries?

Libraries provide the user the benefit to use a variety of files that can be reused in different programs. For example If we were going to use the same functions in different programs, we will just write it once and reuse it instead of having hundreds of copies of the same function, one in each program.

What is a Static library?

A static library is a library that is “copied” into our program when we compile it. Once we have the executable of our program, the library is useless (in other words, it is used for other future projects). We could delete it and our program would continue working, since it has a copy of everything it needs. Only that part of the library that is needed is copied. For example, if the library has two functions and our program only calls one, only that function is copied.

How to create a Static library?

In order to put our code in a library, we need to organize it as follows:

  • One or more source files .c with the code of our functions. To see our files run ls -l *.c.
  • One or more .h header files with the types and prototypes of the functions that we want to be able to use. En este caso nuestro header file se llamará holberton.h.

Once we have our code, to get a static library we must perform the following steps:

Get the object files (.o) from all our sources (.c). For this they are compiled with gcc -Wall -pedantic -Werror -Wextra -c *.c. The -c option tells the compiler not to create an executable, but just an object file.

Using ls -l *.o, we can see all the created object files.

To create the library (.a),which we will call as libholbertonschool.a, use the ar command with the following parameters:

ar -rc libholberton.a *.o

The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to replace older object files in the library, with the new object files.

We can see the object files inside the library runnig the following command:

ar -t libholberton.a

Now, we need to index the created linbrary using the following command:

ranlib libholberton.a

How to use the Static library?

We will use an example to explain this.

First, we create a program where we call one of the functions of the library.

The program will be called main.c and contain the following code:

Note that the chosen function is _puts.

Now we add the library’s name to the command of compilation, using a special flag, normally ‘-l’. Here is an example:

gcc main.c -L. -lholberton -o lexphrase

Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command.

Note also the usage of the ‘-L’ flag — this flag tells the linker that libraries might be found in the given directory (‘.’, referring to the current directory)\

We can see that we created a executable called lexphrase, using the command ls lex*.

Executing lexphrase with the command ./lexphrase:

Bachelor Industrial Engineering