1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<h1 class="subsection">Pattern Rule Examples</h1> <p>Here are some examples of pattern rules actually predefined in <code>make</code>. First, the rule that compiles ‘<samp>.c</samp>’ files into ‘<samp>.o</samp>’ files: </p> <div class="example"> <pre class="example">%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
</pre>
</div> <p>defines a rule that can make any file <samp><var>x</var>.o</samp> from <samp><var>x</var>.c</samp>. The recipe uses the automatic variables ‘<samp>$@</samp>’ and ‘<samp>$<</samp>’ to substitute the names of the target file and the source file in each case where the rule applies (see <a href="automatic-variables">Automatic Variables</a>). </p> <p>Here is a second built-in rule: </p> <div class="example"> <pre class="example">% :: RCS/%,v
$(CO) $(COFLAGS) $<
</pre>
</div> <p>defines a rule that can make any file <samp><var>x</var></samp> whatsoever from a corresponding file <samp><var>x</var>,v</samp> in the sub-directory <samp>RCS</samp>. Since the target is ‘<samp>%</samp>’, this rule will apply to any file whatever, provided the appropriate prerequisite file exists. The double colon makes the rule <em>terminal</em>, which means that its prerequisite may not be an intermediate file (see <a href="match_002danything-rules">Match-Anything Pattern Rules</a>). </p> <p>This pattern rule has two targets: </p> <div class="example"> <pre class="example">%.tab.c %.tab.h: %.y
bison -d $<
</pre>
</div> <p>This tells <code>make</code> that the recipe ‘<samp>bison -d <var>x</var>.y</samp>’ will make both <samp><var>x</var>.tab.c</samp> and <samp><var>x</var>.tab.h</samp>. If the file <samp>foo</samp> depends on the files <samp>parse.tab.o</samp> and <samp>scan.o</samp> and the file <samp>scan.o</samp> depends on the file <samp>parse.tab.h</samp>, when <samp>parse.y</samp> is changed, the recipe ‘<samp>bison -d parse.y</samp>’ will be executed only once, and the prerequisites of both <samp>parse.tab.o</samp> and <samp>scan.o</samp> will be satisfied. (Presumably the file <samp>parse.tab.o</samp> will be recompiled from <samp>parse.tab.c</samp> and the file <samp>scan.o</samp> from <samp>scan.c</samp>, while <samp>foo</samp> is linked from <samp>parse.tab.o</samp>, <samp>scan.o</samp>, and its other prerequisites, and it will execute happily ever after.) </p><div class="_attribution">
<p class="_attribution-p">
Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br>
<a href="https://www.gnu.org/software/make/manual/html_node/Pattern-Examples.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Pattern-Examples.html</a>
</p>
</div>
|