301 lines
9.2 KiB
HTML
301 lines
9.2 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<!-- This HTML file has been created by texi2html 1.54
|
|
from ../texi/make.texinfo on 19 July 2000 -->
|
|
|
|
<TITLE>GNU make - Using make to Update Archive Files</TITLE>
|
|
<link href="make_12.html" rel=Next>
|
|
<link href="make_10.html" rel=Previous>
|
|
<link href="make_toc.html" rel=ToC>
|
|
|
|
</HEAD>
|
|
<BODY>
|
|
<p>Go to the <A HREF="make_1.html">first</A>, <A HREF="make_10.html">previous</A>, <A HREF="make_12.html">next</A>, <A HREF="make_19.html">last</A> section, <A HREF="make_toc.html">table of contents</A>.
|
|
<P><HR><P>
|
|
|
|
|
|
<H1><A NAME="SEC108" HREF="make_toc.html#TOC108">Using <CODE>make</CODE> to Update Archive Files</A></H1>
|
|
<P>
|
|
<A NAME="IDX895"></A>
|
|
|
|
</P>
|
|
<P>
|
|
<STRONG>Archive files</STRONG> are files containing named subfiles called
|
|
<STRONG>members</STRONG>; they are maintained with the program <CODE>ar</CODE> and their
|
|
main use is as subroutine libraries for linking.
|
|
|
|
</P>
|
|
|
|
|
|
|
|
<H2><A NAME="SEC109" HREF="make_toc.html#TOC109">Archive Members as Targets</A></H2>
|
|
<P>
|
|
<A NAME="IDX896"></A>
|
|
|
|
</P>
|
|
<P>
|
|
An individual member of an archive file can be used as a target or
|
|
prerequisite in <CODE>make</CODE>. You specify the member named <VAR>member</VAR> in
|
|
archive file <VAR>archive</VAR> as follows:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
<VAR>archive</VAR>(<VAR>member</VAR>)
|
|
</PRE>
|
|
|
|
<P>
|
|
This construct is available only in targets and prerequisites, not in
|
|
commands! Most programs that you might use in commands do not support this
|
|
syntax and cannot act directly on archive members. Only <CODE>ar</CODE> and
|
|
other programs specifically designed to operate on archives can do so.
|
|
Therefore, valid commands to update an archive member target probably must
|
|
use <CODE>ar</CODE>. For example, this rule says to create a member
|
|
<TT>`hack.o'</TT> in archive <TT>`foolib'</TT> by copying the file <TT>`hack.o'</TT>:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
foolib(hack.o) : hack.o
|
|
ar cr foolib hack.o
|
|
</PRE>
|
|
|
|
<P>
|
|
In fact, nearly all archive member targets are updated in just this way
|
|
and there is an implicit rule to do it for you. <STRONG>Note:</STRONG> The
|
|
<SAMP>`c'</SAMP> flag to <CODE>ar</CODE> is required if the archive file does not
|
|
already exist.
|
|
|
|
</P>
|
|
<P>
|
|
To specify several members in the same archive, you can write all the
|
|
member names together between the parentheses. For example:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
foolib(hack.o kludge.o)
|
|
</PRE>
|
|
|
|
<P>
|
|
is equivalent to:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
foolib(hack.o) foolib(kludge.o)
|
|
</PRE>
|
|
|
|
<P>
|
|
<A NAME="IDX897"></A>
|
|
You can also use shell-style wildcards in an archive member reference.
|
|
See section <A HREF="make_4.html#SEC22">Using Wildcard Characters in File Names</A>. For
|
|
example, <SAMP>`foolib(*.o)'</SAMP> expands to all existing members of the
|
|
<TT>`foolib'</TT> archive whose names end in <SAMP>`.o'</SAMP>; perhaps
|
|
<SAMP>`foolib(hack.o) foolib(kludge.o)'</SAMP>.
|
|
|
|
</P>
|
|
|
|
|
|
<H2><A NAME="SEC110" HREF="make_toc.html#TOC110">Implicit Rule for Archive Member Targets</A></H2>
|
|
|
|
<P>
|
|
Recall that a target that looks like <TT>`<VAR>a</VAR>(<VAR>m</VAR>)'</TT> stands for the
|
|
member named <VAR>m</VAR> in the archive file <VAR>a</VAR>.
|
|
|
|
</P>
|
|
<P>
|
|
When <CODE>make</CODE> looks for an implicit rule for such a target, as a special
|
|
feature it considers implicit rules that match <TT>`(<VAR>m</VAR>)'</TT>, as well as
|
|
those that match the actual target <TT>`<VAR>a</VAR>(<VAR>m</VAR>)'</TT>.
|
|
|
|
</P>
|
|
<P>
|
|
This causes one special rule whose target is <TT>`(%)'</TT> to match. This
|
|
rule updates the target <TT>`<VAR>a</VAR>(<VAR>m</VAR>)'</TT> by copying the file <VAR>m</VAR>
|
|
into the archive. For example, it will update the archive member target
|
|
<TT>`foo.a(bar.o)'</TT> by copying the <EM>file</EM> <TT>`bar.o'</TT> into the
|
|
archive <TT>`foo.a'</TT> as a <EM>member</EM> named <TT>`bar.o'</TT>.
|
|
|
|
</P>
|
|
<P>
|
|
When this rule is chained with others, the result is very powerful.
|
|
Thus, <SAMP>`make "foo.a(bar.o)"'</SAMP> (the quotes are needed to protect the
|
|
<SAMP>`('</SAMP> and <SAMP>`)'</SAMP> from being interpreted specially by the shell) in
|
|
the presence of a file <TT>`bar.c'</TT> is enough to cause the following
|
|
commands to be run, even without a makefile:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
cc -c bar.c -o bar.o
|
|
ar r foo.a bar.o
|
|
rm -f bar.o
|
|
</PRE>
|
|
|
|
<P>
|
|
Here <CODE>make</CODE> has envisioned the file <TT>`bar.o'</TT> as an intermediate
|
|
file. See section <A HREF="make_10.html#SEC97">Chains of Implicit Rules</A>.
|
|
|
|
</P>
|
|
<P>
|
|
Implicit rules such as this one are written using the automatic variable
|
|
<SAMP>`$%'</SAMP>. See section <A HREF="make_10.html#SEC101">Automatic Variables</A>.
|
|
|
|
</P>
|
|
<P>
|
|
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 <TT>`foo.a(dir/file.o)'</TT>, <CODE>make</CODE> will perform
|
|
automatic updating with this command:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
ar r foo.a dir/file.o
|
|
</PRE>
|
|
|
|
<P>
|
|
which has the effect of copying the file <TT>`dir/file.o'</TT> into a member
|
|
named <TT>`file.o'</TT>. In connection with such usage, the automatic variables
|
|
<CODE>%D</CODE> and <CODE>%F</CODE> may be useful.
|
|
|
|
</P>
|
|
|
|
|
|
|
|
<H3><A NAME="SEC111" HREF="make_toc.html#TOC111">Updating Archive Symbol Directories</A></H3>
|
|
<P>
|
|
<A NAME="IDX898"></A>
|
|
<A NAME="IDX899"></A>
|
|
<A NAME="IDX900"></A>
|
|
<A NAME="IDX901"></A>
|
|
<A NAME="IDX902"></A>
|
|
|
|
</P>
|
|
<P>
|
|
An archive file that is used as a library usually contains a special member
|
|
named <TT>`__.SYMDEF'</TT> 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 <TT>`__.SYMDEF'</TT> so that it will summarize the
|
|
other members properly. This is done by running the <CODE>ranlib</CODE> program:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
ranlib <VAR>archivefile</VAR>
|
|
</PRE>
|
|
|
|
<P>
|
|
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,
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
libfoo.a: libfoo.a(x.o) libfoo.a(y.o) ...
|
|
ranlib libfoo.a
|
|
</PRE>
|
|
|
|
<P>
|
|
The effect of this is to update archive members <TT>`x.o'</TT>, <TT>`y.o'</TT>,
|
|
etc., and then update the symbol directory member <TT>`__.SYMDEF'</TT> by
|
|
running <CODE>ranlib</CODE>. 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.
|
|
|
|
</P>
|
|
<P>
|
|
This is not necessary when using the GNU <CODE>ar</CODE> program, which
|
|
updates the <TT>`__.SYMDEF'</TT> member automatically.
|
|
|
|
</P>
|
|
|
|
|
|
<H2><A NAME="SEC112" HREF="make_toc.html#TOC112">Dangers When Using Archives</A></H2>
|
|
<P>
|
|
<A NAME="IDX903"></A>
|
|
<A NAME="IDX904"></A>
|
|
<A NAME="IDX905"></A>
|
|
<A NAME="IDX906"></A>
|
|
|
|
</P>
|
|
<P>
|
|
It is important to be careful when using parallel execution (the
|
|
<CODE>-j</CODE> switch; see section <A HREF="make_5.html#SEC47">Parallel Execution</A>) and archives.
|
|
If multiple <CODE>ar</CODE> commands run at the same time on the same archive
|
|
file, they will not know about each other and can corrupt the file.
|
|
|
|
</P>
|
|
<P>
|
|
Possibly a future version of <CODE>make</CODE> will provide a mechanism to
|
|
circumvent this problem by serializing all commands 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 <CODE>-j</CODE>.
|
|
|
|
</P>
|
|
|
|
|
|
<H2><A NAME="SEC113" HREF="make_toc.html#TOC113">Suffix Rules for Archive Files</A></H2>
|
|
<P>
|
|
<A NAME="IDX907"></A>
|
|
<A NAME="IDX908"></A>
|
|
<A NAME="IDX909"></A>
|
|
<A NAME="IDX910"></A>
|
|
|
|
</P>
|
|
<P>
|
|
You can write a special kind of suffix rule for dealing with archive
|
|
files. See section <A HREF="make_10.html#SEC106">Old-Fashioned Suffix Rules</A>, for a full explanation of suffix rules.
|
|
Archive suffix rules are obsolete in GNU <CODE>make</CODE>, because pattern
|
|
rules for archives are a more general mechanism (see section <A HREF="make_11.html#SEC110">Implicit Rule for Archive Member Targets</A>). But they are retained for compatibility with other
|
|
<CODE>make</CODE>s.
|
|
|
|
</P>
|
|
<P>
|
|
To write a suffix rule for archives, you simply write a suffix rule
|
|
using the target suffix <SAMP>`.a'</SAMP> (the usual suffix for archive files).
|
|
For example, here is the old-fashioned suffix rule to update a library
|
|
archive from C source files:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
.c.a:
|
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
|
$(AR) r $@ $*.o
|
|
$(RM) $*.o
|
|
</PRE>
|
|
|
|
<P>
|
|
This works just as if you had written the pattern rule:
|
|
|
|
</P>
|
|
|
|
<PRE>
|
|
(%.o): %.c
|
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
|
$(AR) r $@ $*.o
|
|
$(RM) $*.o
|
|
</PRE>
|
|
|
|
<P>
|
|
In fact, this is just what <CODE>make</CODE> does when it sees a suffix rule
|
|
with <SAMP>`.a'</SAMP> as the target suffix. Any double-suffix rule
|
|
<SAMP>`.<VAR>x</VAR>.a'</SAMP> is converted to a pattern rule with the target
|
|
pattern <SAMP>`(%.o)'</SAMP> and a prerequisite pattern of <SAMP>`%.<VAR>x</VAR>'</SAMP>.
|
|
|
|
</P>
|
|
<P>
|
|
Since you might want to use <SAMP>`.a'</SAMP> as the suffix for some other kind
|
|
of file, <CODE>make</CODE> also converts archive suffix rules to pattern rules
|
|
in the normal way (see section <A HREF="make_10.html#SEC106">Old-Fashioned Suffix Rules</A>). Thus a double-suffix rule
|
|
<SAMP>`.<VAR>x</VAR>.a'</SAMP> produces two pattern rules: <SAMP>`(%.o):
|
|
%.<VAR>x</VAR>'</SAMP> and <SAMP>`%.a: %.<VAR>x</VAR>'</SAMP>.
|
|
</P>
|
|
<P><HR><P>
|
|
<p>Go to the <A HREF="make_1.html">first</A>, <A HREF="make_10.html">previous</A>, <A HREF="make_12.html">next</A>, <A HREF="make_19.html">last</A> section, <A HREF="make_toc.html">table of contents</A>.
|
|
</BODY>
|
|
</HTML>
|