What are static libraries in C for?

Imagine there is an ideal library, where one could find all information ever existed, and these information are always available for whoever need them.

Alexandria, Egypt

There is no need to store anything locally imagine no possessions, you may say I’m a dreamer ;-)

So, how would that help me to understand library in C?

When you’re working on a new project, chances are parts of the project are already developed either by you or a third party.

How about not reinventing the wheel?

What if there is a way to reuse existing snippets? That would keep your code from growing, reduce coding, compilation and linking time, and facilitate significantly the code maintenance.

C library is a solution that helps you to keep all recurring functions pre compiled, as an executable file.

Such library is called static library. There is also a concept of dynamic library, but let’s keep it for another time.

When you compile a new project, you can link it with a static library, so that any function of that library could be invoked with parameters from your program.

One thing worth mentioning is that the library is linked, so the program executable file is probably lighter than the library itself.

There are 4 main steps to build a static library:

  1. Put all source files into one directory.

Including the header (.h) file containing all the files’s prototypes. Make sure that the header file is only defined once instead of each time it is called.

2. Turning the source code into object code.

Compile all the .c files without linking them. In order to achieve that, we use the -c flag with gcc, which stops the compilation after the assembler generates the object code.

All the C files in the current working directory have been converted into their respective object files.

3. Create a static library.

The command ‘ar’, for ‘archiver’, archives all of the object (.o) files into one static library (.a) file.

The ‘c’ flag tells ‘ar’ to create the library if it doesn’t already exist.

The ‘r’ flag will replace older object files in the library, with the new object files.

A ‘s’ flag indexes the object files in the archive. This is equivalent to the ‘ranlib’ command. In this example below, we won’t be using the ‘s’ flag.

A library “holberton” is created. By convention the library has the prefix “lib” and the suffix “.a”.

4. Index the object files.

If a library has many object files, these files could be indexed to speed up the compiler look up process.

The command for indexing is

Is that all there is?

Now you can compile your “main.c” program with the static library.

In this example, the “main.c” will be using the “_puts” function of the static library “holberton”.

The syntax to do so is:

gcc main.c -L<path_of_lib> -l<name> -o program_code

Remember the library is linked during compiling, so we need to tell gcc where to look for the library.

The -L tells the linker that libraries might be found in the given directory ( here is referring to the current directory).

If you place the library in standard library directories (/usr/lib, /usr/local/lib), there is no need to specify it with the -L flag.

The -l option eliminates the need to type out the “lib” prefix and “.a” extension for the library.

That’s all there is for now.

Thanks for your attention.

Software Engineer student at Holberton School. Reach me @huyxuanminh