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);

 $ 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

 $ 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

00000000 T function1
         U puts

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/, 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 
        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
        rm -rf main testlib.a first.o second.o
Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Multimedia - Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question