Programming Utilities Guide

Building Object Libraries

Libraries, Members, and Symbols

An object library is a set of object files contained in an ar library archive (see ar(1) and lorder(1) in the SunOS reference Manual for details about library archive files.) Various languages make use of object libraries to store compiled functions of general utility, such as those in the C library.

ar reads in a set of one or more files to create a library. Each member contains the text of one file, preceded by a header. The member header contains information from the file directory entry, including the modification time. This allows make to treat the library member as a separate entity for dependency checking.

When you compile a program that uses functions from an object library (specifying the proper library either by filename, or with the -l option to cc), the link editor selects and links with the library member that contains a needed symbol.

You can use ar to generate a symbol table for a library of object files. ld requires this table in order to provide random access to symbols within the library--to locate and link object files in which functions are defined. You can also use lorder and tsort ahead of time to put members in calling order within the library. (See ar(1) and lorder(1) for details.) For very large libraries, it is a good idea to do both.

Library Members and Dependency Checking

make recognizes a target or dependency of the form:

lib.a(member . . . )

as a reference to a library member, or a space-separated list of members.


Note -

Earlier versions of make recognize this notation. However, only the first item in a parenthesized list of members is processed.


In this version of make, all members in a parenthesized list are processed. For example, the following target entry indicates that the library named librpn.a is built from members named stacks.o and fifos.o. The pattern-matching rule indicates that each member depends on a corresponding object file, and that object file is built from its corresponding source file using an implicit rule.

librpn.a:			librpn.a (stacks.o fifos.o)
           		ar rv $@ $?

             $@
librpn.a (%.o): %.o
             @true

When used with library-member notation, the dynamic macro $? contains the list of files that are newer than their corresponding members:

$ make 
cc -c stacks.c 
cc -c fifos.c 
ar rv librpn.a stacks.o fifos.o 
a - stacks.o 
a - fifos.o

Libraries and the $% Dynamic Macro

The $% dynamic macro is provided specifically for use with libraries. When a library member is the target, the member name is assigned to the $% macro. For instance, given the target libx.a(demo.o) the value of $% would be demo.o.

.PRECIOUS: Preserving Libraries Against Removal due to Interrupts

Normally, if you interrupt make in the middle of a target, the target file is removed. For individual files this is a good thing, otherwise incomplete files with brand new modification times might be left in the directory. For libraries that consist of several members, the story is different. It is often better to leave the library intact, even if one of the members is still out-of-date. This is especially true for large libraries, especially since a subsequent make run picks up where the previous one left off--by processing the object file or member whose processing was interrupted.

.PRECIOUS is a special target that is used to indicate which files should be preserved against removal on interrupts; make does not remove targets that are listed as its dependencies. If you add the line:

.PRECIOUS:  librpn.a

to the makefile shown above, run make, and interrupt the processing of librpn.a, the library is preserved.