Managing file interactions with make

Archive libraries

The make program has an interface to archive libraries. A user may name a member of a library in the following manner:

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

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

   projlib::   projlib(pfile1.o)
           $(CC) -c $(CFLAGS) pfile1.c
           $(AR) $(ARFLAGS) projlib pfile1.o
           rm pfile1.o
   projlib::   projlib(pfile2.o)
           $(CC) -c $(CFLAGS) pfile2.c
           $(AR) $(ARFLAGS) projlib pfile2.o
           rm pfile2.o
and so on for each object. This is tedious and error prone. Obviously, 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. (This is true in most cases.)

The make command also gives the user access to a rule for building libraries. The handle for the rule is the .a suffix. Thus, 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, and .s~.a. (The tilde (``~'') syntax will be described shortly.) The user may define other needed rules in the description file.

The above two-member library 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) $@ $(<F:.c=.o)
           rm -f $(<F:.c=.o)
Thus, the $@ macro is the .a target (projlib); the $< and $* macros are set to the out-of-date C language file, and the file name minus the suffix, respectively (pfile1.c and pfile1). The $< macro (in the preceding rule) could have been changed to $*.c.

It is useful to go into some detail about exactly 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. make projlib.

  2. Before makeing projlib, check each dependent of 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). (There are none.)

  5. Use internal rules to try to create projlib(pfile1.o). (There is no explicit rule.) Note that projlib(pfile1.o) has a parenthesis 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 parenthesis implies 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) which fulfills these conditions is .c so the rule is .c.a, and the file is pfile1.c. Set $< to be 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, namely
       @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 parenthesis.
Next topic: Source code control system file names
Previous topic: Summary of default transformation path

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004