The power of a book, or how to use static libraries in C.
This is the second article in a series about what the compilation process is and how it works, using the C language as an example.
- Creating new worlds with GNU Compiler Collection.
- The power of a book, or how to use static libraries in C. (this article)
- Dynamic libraries in C: creating something on what the others will rely.
One of the problems with computer programs, is that they have tendency to grow larger and larger, bringing more challenges for its creators and especially maintainers and increasing time of the compilation and linking (if you need to refresh your knowledge on these topics, please read the first article of the series).
The solution for this problem is in combining out source code into small units of related files, that can be managed easily. And that’s where the concept of library is coming to help.
A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. Normally the library is indexed, so it is easy to find symbols (functions, variables and so on) in them. For this reason, linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds up linking.
Unix systems (as well as most other modern systems) allow us to create and use two kinds of libraries — static libraries and shared (or dynamic) libraries. In this article we will talk more about static ones.
How static library works?
Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime. That’s because object files are also used only during the linking phase, and are not required during runtime — only the program’s executable file is needed in order to run the program.
The basic tool used to create static libraries is a program called
ar (archiver). This program can be used to create static libraries (also known as archive files), modify object files in the static library, list the names of object files in the library, etc.
How to create a static library?
In order to create a static library, we have to perform two steps:
- Create the object files from the source files of the project
- Create the static library (the archive file) from the object files
Let’s assume that we have the project, that recreates some commonly used functions from the C standard library:
Now let’s create the object files with
gcc -c *.c command:
Now we are able to create the library (lets call it libtools)with the
ar -rc command:
ar -rc libtools.a -o *.o
This command creates a static library named
libtools.a and puts copies of the all object files 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. About the flags: 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.
After an archive is created, or modified, we need to index it. This index is later will be used by the compiler to speed up symbol-lookup inside the library. The command used to create or update the index is called
ranlib, and is invoked as follows:
On some systems, the archiver (which is not always
ar) already takes care of the index, so
ranlib is not needed (for example, when Sun's C compiler creates an archive, it is already indexed). However, because
ranlib are used by many makefiles for many packages, such platforms tend to supply a
ranlib command that doesn’t index the archives. This helps using the same makefile on both types of platforms.
How to use a static library?
Here we have the example of how to use the static library during the compilation process:
gcc -o resulted_program our_sources.c -L. -ltools
In this particular case we’ve used
gcc compiler, and asked it to output the result (key
our_sources.c source file and to use library from the current directory (the dot
. after key
-l). Please note that we’ve used just a name
tool without the prefix
lib and the extension
.a. They will be automatically resolved by the compiler.
Lets compile our program and run it:
That’s all, folks!
In this article we looked at the reasons why such a concept as static library exists, how they work, how to create and how to use them.