|
|
The make program has an interface to archive libraries. A user may name a member of a library in the following manner:
projlib(object.o)or
projlib((entry_pt))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.oand 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:
.c.a: $(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-dateAssume the object in the library is out of date with respect to pfile1.c. Also, there is no pfile1.o file.
@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.cThere 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.