How to Create Static Library in Linux ?

If you want to create shared library visit our another article “How to create Shared Library in Linux ?”

Below program demos how to create a static library and link it with the main program to create a executable. For demo, we will write two files / c program each of which has one function defined inside it.

$ vim first.c 
#include <stdio.h>
#include "header.h"

void function1(void) {
        printf("This is function1\n");
}
$ vim second.c 
#include <stdio.h>
#include "header.h"

void function2(void) {
        printf("This is Function2\n");
}
$ vim header.h
#ifndef __HEADER_H
#define __HEADER_H

void function1(void);
void function2(void);

#endif
$ gcc -c first.c 
-rw-rw-r-- 1 myuser myuser 1.1K Sep  1 20:49 first.o
 $ gcc -c second.c
-rw-rw-r-- 1 myuser myuser 1.1K Sep  1 20:49 second.o
 $ ar -cq testlib.a first.o second.o
-rw-rw-r-- 1 myuser myuser 2.3K Sep  1 20:51 testlib.a

Now, lets see what “ar” does. “man ar” shows definition of ar as, “ar – create, modify, and extract from archives”

The GNU ar program creates, modifies, and extracts from archives. An archive is a single file holding a
collection of other files in a structure that makes it possible to retrieve the original individual files
(called members of the archive).
The original files’ contents, mode (permissions), timestamp, owner, and group are preserved in the
archive, and can be restored on extraction.

c Create the archive. The specified archive is always created if it did not exist, when you request an
update. But a warning is issued unless you specify in advance that you expect to create it, by using
this modifier.

q Quick append; Historically, add the files member… to the end of archive, without checking for
replacement.

 $ file testlib.a 
testlib.a: current ar archive
 $ ranlib testlib.a 
$ nm -s testlib.a  
Archive index:
function1 in first.o
function2 in second.o

first.o:
00000000 T function1
         U puts

second.o:
00000000 T function2
         U puts

From “nm” command, we can see that now our created library has the symbols from both the object files first.o and second.o. so, now our static library is ready. Now lets write a main program or application which will use this library and write its own application using library functions as,

 $ vim main.c 
#include <stdio.h>
#include "header.h"
int main(int argc, char *argv[]) {
        function1(); //call function1 from first.c
        function2(); //call function2 from second.c
        return 0;
}
$ gcc -o main main.c testlib.a
 $ file main
main: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=b397310c8ddfabbdbe75f8021bea768321218984, for GNU/Linux 3.2.0, not stripped
$ ./main 
This is function1
This is Function2 

OR You can use the Makefile as below to compile everything and just type “make” in same directory by copying this Makefile.

$ vim Makefile
all:
        gcc -c first.c
        gcc -c second.c
        ar -cq testlib.a first.o second.o
        ranlib testlib.a
        gcc -o main main.c testlib.a
clean:
        rm -rf main testlib.a first.o second.o

If you want to create shared library visit our another article “How to create Shared Library in Linux ?”

1 thought on “How to Create Static Library in Linux ?”

Leave a Comment