D. Creating Static Library with gcc – C Programming Essentials

Appendix D. Creating Static Library with gcc

A static library is a set of functions, which are resolved in a caller at compile time and copied into a target application by a compiler or linker, producing an object file and a standalone executable. All the predefined functions that we use through header files like stdlib.h are part of static libraries. In this article, we will look into the process of creating such static libraries of our own using the GNU C compiler and related utilities, available both for Linux and Windows[1]. Another feature of a static library is its pre-compiled nature. This enables the programmer to distribute the library without having to include the source files. Think of it as something like the printf() function. We use it every day, but we do not need its source code.

We would need a copy of the actual source file, mymath.c, comprising two simple functions: add() and sub() to perform integer addition and subtraction, respectively.

/* mymath.c */
int add (int a, int b)
{
      int sum;
      sum = a+b;
      return sum;
}
int sub (int a, int b)
{
      int diff;
      diff = a-b;
      return diff;
}

Next, we need the header file for this, “mymath.h,” where we have the function declarations of both the functions.

/* mymath.h */
int add (int a, int b);
int sub (int a, int b);

First, we convert the “mymath.c” file to its object file using gcc. The –c option tells gcc to compile the input source file, but not to link it. The –o option is to simply specify a name to the output file.

$ gcc -c mymath.c -o mymath.o

The resultant file “mymath.o” is the object file produced by gcc for our program “mymath.c”. Now, we use the archiever (ar) to produce the static library named “mymath.a” from our “mymath.o” object file. The r, c, and s are options of the ar utility.

  • r: adds the object file(s) (*.o) to the library and replace the object file with same name, if any

  • c: suppresses the message that is written to standard error, when a new archive is created

  • s: forces the regeneration of the archive symbol table

$ ar rcs libmymath.a mymath.o

Note the name of the final library — it begins with “lib” and has an extension “.a”. These are conventions followed in Linux/Solaris/BSD/System V R4. Now that we have created our simple library, let us put it to use. We use the following program along with our library:

/* example.c */
#include <stdio.h>
#include "mymath.h"
int main()
{
       int a, b;
       printf("Enter integer A : "); scanf("%d",&a);
       printf("Enter integer B : "); scanf("%d",&b);
       printf("A+B = %d \nA-B = %d",add(a,b),sub(a,b));
       return 0;
}

We compile this “example.c” using the following commands:

$ gcc example.c -L. -lmymath

The –L flag tells the compiler to add the following directory to the list of directories to be searched for –l. The –l flag instructs the linker to search a standard list of directories for the library, which is actually a file named lib<library-name>.a (libmymath.a here). The output is as expected:

$ ./a.out
Enter integer A : 20
Enter integer B : 10
A+B = 30
A-B = 10

The library file thus obtained by following the above steps can now be distributed along with the corresponding header file. This eliminates the need for distributing the actual source code and also makes the compilation faster, as the library code is pre-compiled and only has to be linked to the new program.



[1] The Windows version of gcc is known as mingw (Minimalistic GNG for Windows). It is freely available from http://www.mingw.org/ and is an open-source software.