summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/make-deduces.html
blob: 747b1840fcd8abce0ec4150b0a422700a04e1457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  <h1 class="section">Letting make Deduce the Recipes</h1>    <p>It is not necessary to spell out the recipes for compiling the individual C source files, because <code>make</code> can figure them out: it has an <em>implicit rule</em> for updating a ‘<samp>.o</samp>’ file from a correspondingly named ‘<samp>.c</samp>’ file using a ‘<samp>cc -c</samp>’ command. For example, it will use the recipe ‘<samp>cc -c main.c -o main.o</samp>’ to compile <samp>main.c</samp> into <samp>main.o</samp>. We can therefore omit the recipes from the rules for the object files. See <a href="implicit-rules">Using Implicit Rules</a>. </p> <p>When a ‘<samp>.c</samp>’ file is used automatically in this way, it is also automatically added to the list of prerequisites. We can therefore omit the ‘<samp>.c</samp>’ files from the prerequisites, provided we omit the recipe. </p> <p>Here is the entire example, with both of these changes, and a variable <code>objects</code> as suggested above: </p> <div class="example"> <pre class="example">objects = main.o kbd.o command.o display.o \
          insert.o search.o files.o utils.o

edit : $(objects)
        cc -o edit $(objects)

main.o : defs.h
kbd.o : defs.h command.h
command.o : defs.h command.h
display.o : defs.h buffer.h
insert.o : defs.h buffer.h
search.o : defs.h buffer.h
files.o : defs.h buffer.h command.h
utils.o : defs.h

.PHONY : clean
clean :
        rm edit $(objects)
</pre>
</div> <p>This is how we would write the makefile in actual practice. (The complications associated with ‘<samp>clean</samp>’ are described elsewhere. See <a href="phony-targets">Phony Targets</a>, and <a href="errors">Errors in Recipes</a>.) </p> <p>Because implicit rules are so convenient, they are important. You will see them used frequently. </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/make-Deduces.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/make-Deduces.html</a>
  </p>
</div>