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++
|libg++.so||g++ specific code|
|libstdc++.so||C++ standard code|
|libc.so||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.
| option || 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.
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)
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++:
To combine several shareable .o files into a shared library you'll use the ld command:
You can use the nm -s command to list all symbols in a .so file, i.e., nm -s libfoo.so.
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
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.