Archive libraries

The make program has an interface to archive libraries. A user can name a member of a library, as in these examples:



The second method actually refers to an entry point of an object file within the library. (The make program looks through the library, locates the entry point, and translates it to the correct object-filename.)

To use this procedure to maintain an archive library, the following type of makefile is required for each object:

   projlib::   projlib(pfile1.o)
           $(CC) -c -O pfile1.c
           $(AR) $(ARFLAGS) projlib pfile1.o
           rm pfile1.o
   projlib::   projlib(pfile2.o)
           $(CC) -c -O pfile2.c
           $(AR) $(ARFLAGS) projlib pfile2.o
           rm pfile2.o
This is tedious and error-prone. In most cases, the command sequences for adding a C language file to a library are the same for each invocation; the file name being the only difference each time.

The make command also gives the user access to a rule for building libraries. The handle for the rule is the .a suffix. A .c.a rule is the rule for compiling a C language source file, adding it to the library, and removing the .o file. Similarly, the .y.a, the .s.a, and the .l.a rules rebuild yacc, assembler, and lex files, respectively. The archive rules defined internally are .c.a, .c~.a, .f.a, .f~.a, .s.a, .s~.a, .C.a, and .C~.a. (The tilde (~) syntax will be described shortly.)

The user may define other needed rules in the makefile.

The two-member library mentioned earlier is then maintained with the following shorter makefile:

   projlib:        projlib(pfile1.o) projlib(pfile2.o)
               @echo projlib up-to-date
The internal rules are already defined to complete the preceding library maintenance. The actual .c.a rule is as follows:
           $(CC) -c $(CFLAGS) $<
           $(AR) $(ARFLAGS) $@ $*.o
           rm -f $*.o
In this example, the $@ macro is the .a target (projlib); the $< and $* macros are set to the out-of-date C language file, and the filename minus the suffix, respectively (pfile1.c and pfile1). The $< macro (in the preceding rule) could have been changed to $*.c.

This is what make does when it sees the construction:

   projlib:    projlib(pfile1.o)
           @echo projlib up-to-date
Assume the object in the library is out-of-date with respect to pfile1.c. Also, there is no pfile1.o file.

  1. Before using make on projlib, check each dependent of projlib.

  2. Enter: make projlib.

  3. projlib(pfile1.o) is a dependent of projlib and needs to be generated.

  4. Before generating projlib(pfile1.o), check each dependent of projlib(pfile1.o).

  5. Use internal rules to try to create projlib(pfile1.o). (There is no explicit rule.) Note that projlib(pfile1.o) has parentheses in the name to identify the target suffix as .a. This is the key. There is no explicit .a at the end of the projlib library name. The parentheses imply the .a suffix. In this sense, the .a is hard-wired into make.

  6. Break the name projlib(pfile1.o) up into projlib and pfile1.o. Define two macros, $@ (=projlib) and $* (=pfile1).

  7. Look for a rule .X.a and a file $*.X*. The first .X (in the .SUFFIXES list) that fulfills these conditions is .c so the rule is .c.a, and the file is pfile1.c. Set $< to pfile1.c and execute the rule. In fact, make must then compile pfile1.c.

  8. The library has been updated. Execute the command associated with the projlib: dependency. That is:
       @echo projlib up-to-date

It should be noted that to let pfile1.o have dependencies, the following syntax is required:
   projlib(pfile1.o):        $(INCDIR)/stdio.h  pfile1.c
There is also a macro for referencing the archive member name when this form is used. The $% macro is evaluated each time $@ is evaluated. If there is no current archive member, $% is null. If an archive member exists, then $% evaluates to the expression between the parentheses.

Next topic: Tildes in SCCS filenames
Previous topic: Summary of default transformation path

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003