130 GNU make
cc -c bar.c -o bar.o
ar r foo.a bar.o
rm -f bar.o
Here make has envisioned the file bar.o as an intermediate file. See Section 10.4 [Chains of
Implicit Rules], page 117.
Implicit rules such as this one are written using the automatic variable ‘$%’. See
Section 10.5.3 [Automatic Variables], page 120.
An archive member name in an archive cannot contain a directory name, but it may
be useful in a makefile to pretend that it does. If you write an archive member target
foo.a(dir/file.o), make will perform automatic updating with this recipe:
ar r foo.a dir/file.o
which has the effect of copying the file dir/file.o into a member named file.o. In
connection with such usage, the automatic variables %D and %F may be useful.
11.2.1 Updating Archive Symbol Directories
An archive file that is used as a library usually contains a special member named __.SYMDEF
that contains a directory of the external symbol names defined by all the other members.
After you update any other members, you need to update __.SYMDEF so that it will sum-
marize the other members properly. This is done by running the ranlib program:
ranlib archivefile
Normally you would put this command in the rule for the archive file, and make all the
members of the archive file prerequisites of that rule. For example,
libfoo.a: libfoo.a(x.o) libfoo.a(y.o) ...
ranlib libfoo.a
The effect of this is to update archive members x.o, y.o, etc., and then update the symbol
directory member __.SYMDEF by running ranlib. The rules for updating the members are
not shown here; most likely you can omit them and use the implicit rule which copies files
into the archive, as described in the preceding section.
This is not necessary when using the GNU ar program, which updates the __.SYMDEF
member automatically.
11.3 Dangers When Using Archives
It is important to be careful when using parallel execution (the -j switch; see Section 5.4
[Parallel Execution], page 47) and archives. If multiple ar commands run at the same time
on the same archive file, they will not know about each other and can corrupt the file.
Possibly a future version of make will provide a mechanism to circumvent this problem
by serializing all recipes that operate on the same archive file. But for the time being, you
must either write your makefiles to avoid this problem in some other way, or not use -j.
11.4 Suffix Rules for Archive Files
You can write a special kind of suffix rule for dealing with archive files. See Section 10.7
[Suffix Rules], page 125, for a full explanation of suffix rules. Archive suffix rules are
obsolete in GNU make, because pattern rules for archives are a more general mechanism
Comentários a estes Manuais