Static Libraries in C

Kevin Apostol
5 min readOct 14, 2019

--

Why use libraries?

A library is a file that contains several object files. It can be used as a single entity in the linking phase during the compilation process of a program.

A library’s role:

  • It provides you to use functions without writing them yourself.
  • Normally the library is indexed. So it is easy to find symbols (functions, variables and so on) in them.
  • Linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk.
  • When using a library, we have fewer files to look for and open, which even further speeds the linking process.

By convention, they have the prefix lib and the suffix .a — for example

libmynewlibrary.a

How do they work?

We can create two types of libraries: static and shared libraries. The first discussed, the object files are installed into the compiled file before the program can be run. The second mentioned, the library is loaded into memory at runtime, by a system called dynamic loader. It checks out which shared libraries were linked with the program, loads them to memory, and attaches them to the copy of the program in memory.

In this post, we will be talking about how static libraries work and how to create them.

Static libraries are just collections of object files that are linked into the program during the linking phase of the compilation process and are not relevant during runtime. Only the program’s executable file is needed in order to run the program.

How to create them?

For our example, we have .c files in the proto directory:

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

gcc -c [.c file/s]

By using the -c flag, it will compile or assemble the source files but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o.

Unrecognized input files, not requiring compilation or assembly, are ignored.

For our example we use:

gcc -c *.c

This is because we want all of our .c files to create all .o (object) files. After running the script above, now we get to see files that end with .o

Now we're ready to create our static library!

To create one we use a program called 'ar', meaning 'archiver'. This program can be used to create static libraries, modify object files in the static library, list the names of object files in the library, and so on. Libraries are actually archive files. In order to create a static library, we can use the command:

ar -rc libmylib.o myobj1.o myobj2.o

This command creates a static library libmylib.o and puts copies of the object files myobj1.o, and myobj2.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.

In our example above, we want to create a library libholberton.a and add all of the object files to it. Hence, we run the script:

ar -rc libholberton.h *.o

After running the above script we now get to see a newly created archive file libholberton.a

We now can remove all the .o and .c files since we got all of the objects archived to a .a file.

We can run ar with the -t flag to display a table of all the object files we inserted.

We can also use the nm command to list the symbols from the object files:

How to use them?

We have a test case main.c

After we created our archive, we want to use it in a program. This is done by adding the library’s name to the list of object file names given to the linker, using a special flag, normally '-l library name without lib prefix and .a extension.

gcc main.c -L. -lholberton -o quote

Lets put this script into action and run the executable file

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 the 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 ('.', referring to the current directory), in addition to the standard locations where the compiler looks for system libraries.

--

--