C static libraries.

Dany Puertas
4 min readMar 1, 2021

What are libraries in C programming?

“Good code is your best documentation” — Steve McConnell

A library in C is a collection of header files, exposed for use by other programs. The library therefore consists of an interface expressed in a .h file (named the "header") and an implementation expressed in a .c file. This .c file might be precompiled or otherwise inaccessible, or it might be available to the programmer. (Note: Libraries may call functions in other libraries such as the Standard C or math libraries to do various tasks.)

The format of a library varies with the operating system and compiler one is using. For example, in the Unix and Linux operating systems, a library consists of one or more object files, which consist of object code that is usually the output of a compiler (if the source language is C or something similar) or an assembler (if the source language is assembly language).

Why should you 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 we make computer programs, we realize that some parts of the code are used in many of them. For example, we can have several programs that use complex numbers and the functions of addition, subtraction, etc. are common. It is also possible, for example, that we like to play games, and we realize that we are repeating over and over the code to move an image (a little Martian or Lara Croft) on the screen.

It would be great to be able to put these functions in a separate directory from the specific programs and have them already compiled, so that we can use them whenever we want. The huge benefits of this are:

Not having to rewrite the code (or copy-paste).
We will save the time of compiling each time that code that is already compiled. In addition, we already know that while we make a program, we test and correct, it is necessary to compile between many and “more many” times.
The already compiled code will be tested and reliable. Not the first times, but when we have already used it in 200 different programs and we have corrected the errors.

How they work?

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.

To better understand how it works, A static library “fits” into our executable, which means that we can take it to another computer without fear of missing libraries.

How to create a static library?

To create a static library using GCC we need to compile our library code into an object file so we tell GCC to do this using -c:

Here in the above command , all the .c extension files( C files) in the current working directory have been converted in to their respective object files.

2. The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file.

3. If needed use the ‘ranlib <libraryname.a> to index the library, This is useful for linking and objects calling each other.

4. If we want to see the contents of our library, we can use the ar option -t.

this should look like this …

How to use them:

--

--