Creating Unix Libraries

In Unix (and some other operating systems) a library is a collection of related object files group together. Libraries can be linked with other libraries and other object files to create executables.

When you create a C++ program, for example, you'll use several libraries even if you don't know you're using them. Here are some of the libraries linked automatically by g++
library use/purpose g++ specific code C++ standard code math library standard C library code

The table shows shared or dynamic versions of the libraries which typically have a .so suffix. There are also static versions that have a .a suffix instead of a .so suffix, e.g., tapestry is installed as libtapestry.a on the acpub system.

You can build a library from .o files using different commands depending on whether you want to build a static or dynamic library. The information below is a bare minimum, but enough to get a library built so that you can supply a user with a single library instead of multiple .o files. You'll probably still need to supply many header files.

Static Libraries

To build a static library, compile all source files into .o files then use the command ar to archive a library of the .o files. You can use man ar to see all the options, a minimal set is described below.

option purpose
c create a new library
q add the named file to the end of the archive
r replace a named archive/library member
t print a table of archive contents

For example: ar cq libfoo.a *.o creates a new library named libfoo.a from all .o files in a directory. Normally you'll use the ar command in a makefile so you'll probably use a makefile variable rather than *.o.

On non-solaris machines you may need to run the command ranlib on a library in order to build an index or table of contents for the library.

Static Libraries and Makefiles

On the acpub system in ~ola/libMakefile you can find Makefile that was used in 2003 to compile the Tapestry libraries. This shows how to compile several .cpp files into .o files and link these into a static library. The relevant lines are reproduced below. These lines show

LIB_FILES = tstring.cpp bigint.cpp strutils.cpp ctimer.cpp date.cpp \
             more .cpp source files listed as needed


# All source files have associated object files
LIBOFILES		= $(LIB_FILES:%.cpp=%.o)       

# all is the default rule
all	: libtapestry.a

# remove the old tapestry library and remake the new one
libtapestry.a:	$(LIBOFILES)
	rm -f $@
	ar cq $@ $(LIBOFILES)

Dynamic libraries

Dynamic libraries are linked when a program is run rather than when a program is compiled. This decreases the size of stored executables and also permits newer versions of libraries to be linked at run time.

Two steps are needed to create a shared library. First all source files must be compiled as sharable objects. To do this use the -fPIC flag to g++:

g++ -fPIC -o foo compiles into a sharable foo.o.

To combine several shareable .o files into a shared library you'll use the ld command:

ld -G *.o -o combines all .o files into a shared library named The -o option specifies an output file, the -G option builds a shared library in which unresolved references are allowed.

You can use the nm -s command to list all symbols in a .so file, i.e., nm -s

Using libraries

Libraries traditionally begin with a prefix of lib, followed by the name of the library, followed by the suffix: either .a or .so. For example: libm.a, libg++.a, libtapestry.a are archive files for libraries m, g++, and tapestry.

When you compile the -l flag is used to specify libraries. A prefix of lib is assumed so that

g++ -o foo -ltapestry assumes a library named libtapestry --- the .a or .so will be determined appropriately by which is found or how the compiler is configured.

Note, by convention (and need) all libraries start with a lib prefix, but when g++ or another compiler is used the -lnoprefix form is used, i.e., as shown above to invoke the library stored in libtapestry.a you use -ltapestry.

Owen L. Astrachan
Last modified: Sun Feb 16 19:25:22 EST 2003