Creating Static 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.

READ  Tricks for optimising website download speed - part 2 - Image optimisations

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 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=bfd120391ece58f613a9bd5cee0230c5ad0459fa, 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

Leave a Reply

Your email address will not be published. Required fields are marked *