How to create a static archive library in C++ (G++)

Get FREE domain for 1st year and build your brand new site

Binary Tree Problems books

To create a static or archive library in C++ using G++, compile the C++ library code using GCC/ G++ to object file and convert the object file to archive file using ar. The code can be converted to executable by linking the archive file using G++.

// Convert library code to Object file
g++ -c -o library.o library.c

// Create archive file/ static library
ar rcs library.a library.o

Archive library is to transfer the library code to the executable so that the executable does not depend on external library dependencies. This increases the size of the executable.

There are four steps:

  • Compile C++ library code to object file (using g++)

  • Create archive file using object file (ar)

  • Compile the C++ code using the header library file using the archive library (using g++)

  • Run the executable (using a.out)

  • Step 1: Compile C code to object file

gcc -c -o code.o code.c
  • Step 2: Create archive file using object file
ar rcs library.a code.o

ar is an utility that is used to create, modify and extract archive files. The "rcs" signify the following:

  • r: insert object file by replacing existing code

  • c: create new archive file

  • s: write an index

  • Step 3: Compile C++ code

g++ -std=c++14 code.cpp library.a
  • Step 4: Run the archive code
./a.out

This involves four major files:

  • library.hpp: Library header file
  • library.cpp: Library C++ file
  • library.o: Object file of library.cpp
  • library.a: Archive file/ static library of the above library
  • code.cpp: C++ code using the library through header file
  • a.out: executable

Example

In this example, we will create a C++ library and use it in a C++ code. We will build our library as an archive library and link it to our code to generate an executable independent of library.

Our library will have a simple function print_value() which will take in an Integer and print it to the console.

Header file "library.hpp":

#ifndef LIB1_H_INCLUDED
#define LIB1_H_INCLUDED

#ifdef __cplusplus
   extern ā€œCā€ {
#endif

void print_value ( int x );

#ifdef __cplusplus
   }
#endif

#endif /* LIB1_H_INCLUDED */

Let us create the code for the above library header file in the code file "library.cpp":

#include "library.hpp"
#include <stdio.h>

void print_value( int x )
{
    cout << x << "\n";
}

Save the code as library.cpp. Compile the above code as:

g++ -std=c++14 -c -o library.o library.cpp

Create the archive file library.a using the object file:

ar rcs library.a library.o

Using the library

#include <stdio.h>
#include "library.hpp"

int main ( void )
{
    print_value(10);
    return 0;
}

Create the executable:

g++ -std=c++14 code.cpp library.a

This will create the executable a.out which will run on any compatible machine without the header/ library files as we have inserted the library file within the executable.

./a.out

With this, we have created an archive file and generated an executable using it. Enjoy.