Skip to main content

16.4) Creating The Archive


The process for bundling object code into a library archive is system-dependent. On UNIX-like systems, you can use the ar command line tool, with the following options:

  • c, to create the archive if it doesn’t exist already
  • r, to replace object code files
  • s, to add a symbol table to the archive
  • v, to operate verbosely, printing information on progress

So, assuming that are functions are in source files data.cpp and stats.cpp as discussed previously, we can create the object code and bundle it as a single archive like so:

g++ -c data.cpp
g++ -c stats.cpp
ar -crsv libdata.a data.o stats.o

The library now consists of two files: header file data.hpp and library archive libdata.a.