Understanding C Static Libraries

Diego Jeanluck Linares Castillo
5 min readMar 3, 2022
C Logo
C Logo

Before talking about the Libraries you must take into account the process of compilation. If you don’t know about that I recommend reading this article before this.

Well with that into account we are prepared to begin:

Types of Libraries

Exists two types of libraries on C:

  • Static libraries
  • Shared / Dynamic libraries

A library is just a file that contains a collection of ordinary object files (.o files) that are linked into the program during the linking phase of the compilation.

Why use Libraries?

We use libraries to reuse the code and not go to the directories to copy the code or the same file and paste it into our current file or actual directory.

So here come libraries to the rescue, we can use them to access our functionality instead of using a library made by someone else. If we do this we can write less code and commands as well as get more order in general.

In addition, we can create the libraries mainly to be more organized or to work as a team on large projects it is easier to work using libraries.

Some disadvantages and advantages about static libraries:

Advantage:

  • The executable file doesn’t depend on any library, you don’t need to install anything to run the program. Gives more portability to a program but at the expense of larger

Disadvantages:

  • What happens when we call a lot of functions like 100 or 200, well if we are using static linking (static libraries). The result of this is a higher size of the resulting executable file.
  • Another problem is with the updates to the library if we change a little function of the library we have to recompile the executable file and update it.

How it’s works?

How works the static linking

Static Linking

In the phase of linking, if we are using a function of a static library and this function exists in our header file, the compiler will go to search the symbol of that function in the static library.

The symbols are the metadata about the address of the object files

To finally add the library code to our source code and get the output of the compiler. The executable file is in machine code (binary code) with the source code and the library code inside it.

How to create them?

Simply way to create a static library

1. Get The Object code (.o)

First, we have to compile our C programs and get the object code (.o files)

gcc *.c -c

This command compiles all .c files of the current directory and stops the compilation before the phase of linking, in this way we can get the object code.

2. Create the library

For this step, we must use the command “ar” (archive) in the terminal.

Use the command “man ar” for more information.

ar -rc *.o <name_of_the_library>

Well, step by step we are going to understand the snippet of code:

  • ar = Creates|Modify|Extract form archives.
  • -r = This flag is for replace, in case this flag found some file with the same name and replace it for the recent version.
  • -c = Create, The library will be created without this flag if this file doesn’t exist but appears a message of warning to evade that we use this flag to confirm that we really want to create that library.
  • *.o = Make reference to all .o files in the current directory
  • <name_of_the_library> = This can sound obvious but is important for the libraries to use the prefix “lib” and the extension “.a” to use them, so we have to take into account this.

3. Index the library

After creating or modifying a library is necessary to index it we do this with the command “ranlib”, this step is important for the compilator because this uses indexes to speed up the search of the symbols on the library and secure their order.
To do this we have to use the next command on the terminal.

ranlib <name_of_the_library>
#or
ranlib -t <name_of_the_library>

The flag -t is for updating the timestamp of the symbol map of the archive.

4. Check the contents of the library (List the symbols)

Here is the last step, after all of this we are going to use the next command on the terminal “nm”. This command list the symbols from the object files (.o) or in this case of our library.

nm <name_of_the_library>

And voila! you created your first library but that’s not all now we’re going to learn how to use our new library so let’s go for it.

How to use them?

Well after creating our library we have to use it and for that, we have to use a C file (.c) that uses 1, 2, or more functionalities created by ourselves.

1. Create our main.h file

The .h file is an header file, this file contents the prototypes of our programs or even libraries like <stdio.h> or <string.h>, etc.
You need to have something like this on your header file.

#include <stdlib.h>
#include <stdio.h>
int _putchar(char c);
char *create_array(unsigned int size, char c);
char *_strdup(char *str);
char *str_concat(char *s1, char *s2);
int **alloc_grid(int width, int height);

To use this header file we have to use the next line on our C files in case of our header file was called main.h.

#include "main.h"

After this line, we can use the prototypes (functions) and the above-mentioned libraries.
For example:

#include "main.h"int main(void)
{
printf("Hello World");
return (0);
}

We use the <stdio.h> library but in our C file (.c) are not included but yes in our header file. But is for that we can use printf because the main.h calls to the <stdio.h> library.

2. Create our test file

In this step, you need to create a C file (.c) that uses the header file and any of the prototypes.

3. Compile using our library!

Finally, we’re here so for compile our file using the library using the next command.

gcc <c.file> -L. -l<name_of_library>

Well, step by step we are going to understand the command:

  • gcc = GNU Compiler Collection this is the compiler we’re going to use.
  • -L. = This flag is to indicate where the library is located, the dot is for saying that the library is in the current directory.
  • -l<name_of_library> = This maybe sounds obvious but this flag indicates that the next command will be a library. You have to write the file the next way (in case of our library’s name is libtest.a).
gcc main.c -L. -ltest

We have to write the library this way because the “-l” flag is like that we have lib”…”.a and we have to complete the name of the library, in this case, is with test because the library name is “libtest.a”.

Thank you so much for reading.

--

--

Diego Jeanluck Linares Castillo

Autodidact | Passionate about technology | Holberton School Student