C Libraries

Why use libraries?

Programmers usually look for the way for the computer to do for us the things that we would have to repeat ourselves, again and again, here is the reason to create libraries, which are collections of functions that we use frequently, this allows programmers to reuse their code, saving unnecessary hours of work, and creating more efficient code.

How they work?

Unix systems allow us to create and use two kinds of libraries: Static Libraries and Dynamic Libraries.

Static libraries

They are linked when compiling a program, in this way, the library is together with the compiled source code of the program, in a single executable.

These types of libraries have certain advantages:
— Only the functionality to be used by the program is copied.

— They allow faster execution, since the executable code of the function is found along with the program executable.

There are also certain disadvantages:
— The final executable code is larger, because the function is copied.

— A change in the sources of a static library, forces to recompile the programs that make use of that library.

Dynamic Libraries

They are libraries that link when you run a program. The library is not left with the compiled source code of the program, but that its functionality is compiled in a separate file.

Advantages of a dynamic library:
— The final executable code of a program that makes use of a library dynamics is smaller, because the functionality of the bookstore in this.

— Changes made over a dynamic library do not force recompile each of the programs that use it.

Some disadvantages:
— They can provide a slower execution, since the executable code of the functionality is in a file outside the executable of the program that uses it.

— Programs must necessarily be moved along with all dynamic libraries that you use.

How to create them

The object files must be previously generated with gcc and the -c flag:

gcc -c betty_file.c

We will see how to create static libraries through ar program:

ar rc libholberton.a betty_file.o

This command creates a static library named ‘holberton.a’ and puts copies of the object file “betty_file.o” in it. If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the library. 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.

After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation:

ranlib libholberton.a

How to use them

gcc main.c -L. lholberton -o function

This will create a program using “main.c”, and any symbols it requires from the “holberton” static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the ‘-L’ flag — this flag tells the linker that libraries might be found in the given directory (‘.’)



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