1
2
3
4
5
6
7
8
9
10
11
|
<h1 class="section">Using Implicit Rules</h1> <p>To allow <code>make</code> to find a customary method for updating a target file, all you have to do is refrain from specifying recipes yourself. Either write a rule with no recipe, or don’t write a rule at all. Then <code>make</code> will figure out which implicit rule to use based on which kind of source file exists or can be made. </p> <p>For example, suppose the makefile looks like this: </p> <div class="example"> <pre class="example">foo : foo.o bar.o
cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
</pre>
</div> <p>Because you mention <samp>foo.o</samp> but do not give a rule for it, <code>make</code> will automatically look for an implicit rule that tells how to update it. This happens whether or not the file <samp>foo.o</samp> currently exists. </p> <p>If an implicit rule is found, it can supply both a recipe and one or more prerequisites (the source files). You would want to write a rule for <samp>foo.o</samp> with no recipe if you need to specify additional prerequisites, such as header files, that the implicit rule cannot supply. </p> <p>Each implicit rule has a target pattern and prerequisite patterns. There may be many implicit rules with the same target pattern. For example, numerous rules make ‘<samp>.o</samp>’ files: one, from a ‘<samp>.c</samp>’ file with the C compiler; another, from a ‘<samp>.p</samp>’ file with the Pascal compiler; and so on. The rule that actually applies is the one whose prerequisites exist or can be made. So, if you have a file <samp>foo.c</samp>, <code>make</code> will run the C compiler; otherwise, if you have a file <samp>foo.p</samp>, <code>make</code> will run the Pascal compiler; and so on. </p> <p>Of course, when you write the makefile, you know which implicit rule you want <code>make</code> to use, and you know it will choose that one because you know which possible prerequisite files are supposed to exist. See <a href="catalogue-of-rules">Catalogue of Built-In Rules</a>, for a catalogue of all the predefined implicit rules. </p> <p>Above, we said an implicit rule applies if the required prerequisites “exist or can be made”. A file “can be made” if it is mentioned explicitly in the makefile as a target or a prerequisite, or if an implicit rule can be recursively found for how to make it. When an implicit prerequisite is the result of another implicit rule, we say that <em>chaining</em> is occurring. See <a href="chained-rules">Chains of Implicit Rules</a>. </p> <p>In general, <code>make</code> searches for an implicit rule for each target, and for each double-colon rule, that has no recipe. A file that is mentioned only as a prerequisite is considered a target whose rule specifies nothing, so implicit rule search happens for it. See <a href="implicit-rule-search">Implicit Rule Search Algorithm</a>, for the details of how the search is done. </p> <p>Note that explicit prerequisites do not influence implicit rule search. For example, consider this explicit rule: </p> <div class="example"> <pre class="example">foo.o: foo.p
</pre>
</div> <p>The prerequisite on <samp>foo.p</samp> does not necessarily mean that <code>make</code> will remake <samp>foo.o</samp> according to the implicit rule to make an object file, a <samp>.o</samp> file, from a Pascal source file, a <samp>.p</samp> file. For example, if <samp>foo.c</samp> also exists, the implicit rule to make an object file from a C source file is used instead, because it appears before the Pascal rule in the list of predefined implicit rules (see <a href="catalogue-of-rules">Catalogue of Built-In Rules</a>). </p> <p>If you do not want an implicit rule to be used for a target that has no recipe, you can give that target an empty recipe by writing a semicolon (see <a href="empty-recipes">Defining Empty Recipes</a>). </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/Using-Implicit.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Using-Implicit.html</a>
</p>
</div>
|