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 |
---|---|
libg++.so | g++ specific code |
libstdc++.so | C++ standard code |
libm.so | math library |
libc.so | standard C library code |
The table shows shared or dynamic versions of the libraries, there are also static versions that have a .a suffix instead of a .so suffix.
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.
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