C Static Libraries

C Static Libraries

¿Why use libraries?

Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use. Functions in a C library can be used and accessed by programmers to create several different programs.

As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

¿How they work?

A static library is a file containing a collection of object files (*.o) that are linked into the program during the linking phase of compilation and are not relevant during runtime.

when a program is compiled, the compiler generates an object file from a source file. After generating the object file, the compiler also invokes the Linker. The role of the linker, in this case, is to copy the code of the library to our object file.

Basically, static libraries are just a collection of object files that are merged by the linker with another object file to form a final executable.

Conventionally, they start with “lib” and end with “.a” or “.lib” (depending on your platform).

¿How to create them?

To create a static library you have to use the ‘ar’ or archiver program. The idea is that these functions are being archived until needed. A function saved in .c format is recompiled into an object file, .o .

Creating a Static Library step by step:

  1. Create all your source files. Source files hold any functions you will use.
  2. Compile the source files into object files. Using GCC use this command:

No hay texto alternativo para esta imagen

3. Create a static library. Using “libmy” as an example of a library name, this command creates a Static library:

No hay texto alternativo para esta imagen

4. The ‘ar’ is the program being used to archive the files. The ‘c’ tells the program to create a library. The ‘r’ tells the program the replace or update older files in the library. With step three a static library has been created. If needed use the ‘ranlib <libraryname.a> to index the library.

No hay texto alternativo para esta imagen

Indexing a library will let the compiler know just how old a library file is. Indexing also gives the library file a header and makes it easier for the compiler to reference the symbols. This step may or may not be necessary depending on your computer system.

To view the contents of the static library access it using: ‘ar -t libholberton.a’ in the command line.

¿How to use them?

We have created a static library and can now use it.

we have a program:

No hay texto alternativo para esta imagen

In the header: “main.h” contains function and function definitions used to tell the compiler how to call functionality. It contains “data types and constants used with the libraries”(geekforgeeks). When we compile the file we call the library:

No hay texto alternativo para esta imagen

Now we can run the executable:

No hay texto alternativo para esta imagen

If the library is working properly the output should be:

No hay texto alternativo para esta imagen

We have created a functioning static library.


To view or add a comment, sign in

More articles by Angel Piva

  • Ahorrapp - Holberton School

    Do you like saving money? Do you think this habit is useful to improve economy? Well, Ahorrapp is now available!…

    2 Comments
  • What happens when you type google.com in your browser and press Enter

    Everyone has done a search for something in their browser at some point, but not many people know what´s happening in…

  • Dynamic Libraries in C

    In my article located below, I gave a detailed explanation of how to create and use dynamic libraries. If you need a…

  • What happens when you type `ls -l *.c` in the shell?

    This blog was thought for those people who don´t know about what is a shell and what does it does, so, if you want to…

Others also viewed

Explore content categories