DEV Community

Clifford Mapesa
Clifford Mapesa

Posted on • Originally published at cliffordmapesa.hashnode.dev on

Getting Started With Static Libraries In C

Introduction

To understand how static libraries work let's first look at the compilation process of a file in C programming. Compilation is when code is transformed from human-readable language to machine language. The software that is used for this conversion is known as a Compiler, e.g. gcc.

The C compilation follows these stages:

1. Preprocessing

This is the first stage where the source code is passed. The source code is made ready for compilation. The source code is expanded (adds the required information) and then this expanded source code is passed on to the compiler.

2. Compilation

Once preprocessing is complete, the actual compilation begins. The compiler takes the C code and translates it into assembly code, specific to the target platform. Assembly code represents a low-level view of the program, consisting of instructions that the processor can execute. The source file which got the ( .i ) extension in the previous step gets converted into ( .s ) extension by the compiler.

3. Assembly

Next, the assembler converts the assembly code into machine code, also known as object code. The object code consists of binary representations of instructions and data. This step is a crucial bridge between human-readable code and the language the computer understands. -The extension of the file in this step becomes ( .obj ).

4. Linking

A linker is a tool that is used to link all the parts of a program together in order of execution. The Linker plays a very important role. If your C program includes a header file, and you are using some function defined in that header file, then the Linker will ink the required object code for the function in the library, to the object code of your program and package them together.

Note: To remember the compilation process use PCAL mnemonic.

Figure: Compilation process of a C program

A library is a file containing several object files that can be used as a single entity in the linking phase of a program. A static library is a collection of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime.

How to create a static library

Step 1

The first step is to compile our code using a compiler e.g. gcc:

gcc -c *.c

Enter fullscreen mode Exit fullscreen mode
  • gcc is the compiler

  • The -c flag informs gcc to compile our C files without the linking phase

  • The argument .c* tells gcc to compile ALL files whose names end with .c extension

Step 2

The next step is to use a program called ar for "archiver" to create a static library. 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. In order to create a static library, we can use a command like this:

ar -rc libexample.a *.o

Enter fullscreen mode Exit fullscreen mode

* ar is the name of a program that creates static libraries.

  • The r flag tells it to insert files into the archive, if a file with the same name exists it will be replaced.

  • The c flag tells ar to create the library if it doesn't already exist. If it does exist, the command will proceed, replacing existing files with new ones.

  • libexample.a is the name of the library. The prefix " lib" is the naming convention. The .a extension is commonly used for static libraries in Unix-like systems.

  • .o this matches all files with the extension .o in the current directory. These are the object files that will be added to the archive. Object files are typically the result of compiling source code files.

Step 3

After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol lookup inside the library, and to make sure that the order of the symbols in the library won't matter during compilation (this will be better understood when we take a deeper look at the link process at the end of this tutorial). The command used to create or update the index is called 'ranlib', and is invoked as follows:

ranlib libexample.a

Enter fullscreen mode Exit fullscreen mode

If we want to see object files present in our static library we use the following command:

ar -t libexample.a

Enter fullscreen mode Exit fullscreen mode
  • ar: This is the archive command in Unix-like operating systems.

  • -t: This option stands for "table of contents." It is used to display the names of the files (object files) contained in the archive without extracting them.

  • libexample.a: This is the name of the static library for which you want to view the table of contents.

We can also see the symbols and associated information in our library, using the command nm

nm libexample.a

Enter fullscreen mode Exit fullscreen mode

How to use a C Library in a program

When compiling your main program, specify the static library using the -l flag (for library) and provide the library name (without the "lib" prefix and ".a" extension). For example:

gcc main.o -o my_program -L. -lexample

Enter fullscreen mode Exit fullscreen mode

`

  • main.o: Your main program's object file.

  • -o my_program: Specifies the output executable name.

  • -L.: Instructs the linker to look for libraries in the current directory.

  • -lexample: Links with the libexample.a static library.

How to run our program

The command we can use, after all these steps, is:

`
./my_program

`

Advantages and Disadvantages

Advantages

  • Simplicity: Easy to use an deploy

  • Performance: Generally faster execution since everything is resolved at compile time

  • Independence: No external dependencies during runtime

Disadvantages

  • Size: Can lead to larger executables

  • Updates: Requires recompilation if the library is modified.

  • Shared Memory: Multiple instances of a program share the same code in memory

When to use static libraries

  • When performance is critical and the size of the executable is not a significant concern

  • Suitable for small to medium-sized projects, where simplicity and independence are valued.

Conclusion

In summary, static libraries are a powerful tool for code organization, reuse, and distribution. They simplify the process of including external code in your projects and provide a way to create standalone executables. However, developers need to consider trade-offs, such as larger executables and the need for recompilation when making changes to the library.

References

Top comments (0)