summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/simple-makefile.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gnu_make/simple-makefile.html
new repository
Diffstat (limited to 'devdocs/gnu_make/simple-makefile.html')
-rw-r--r--devdocs/gnu_make/simple-makefile.html35
1 files changed, 35 insertions, 0 deletions
diff --git a/devdocs/gnu_make/simple-makefile.html b/devdocs/gnu_make/simple-makefile.html
new file mode 100644
index 00000000..a32271a4
--- /dev/null
+++ b/devdocs/gnu_make/simple-makefile.html
@@ -0,0 +1,35 @@
+ <h1 class="section">A Simple Makefile</h1> <p>Here is a straightforward makefile that describes the way an executable file called <code>edit</code> depends on eight object files which, in turn, depend on eight C source and three header files. </p> <p>In this example, all the C files include <samp>defs.h</samp>, but only those defining editing commands include <samp>command.h</samp>, and only low level files that change the editor buffer include <samp>buffer.h</samp>. </p> <div class="example"> <pre class="example">edit : main.o kbd.o command.o display.o \
+ insert.o search.o files.o utils.o
+ cc -o edit main.o kbd.o command.o display.o \
+ insert.o search.o files.o utils.o
+
+main.o : main.c defs.h
+ cc -c main.c
+kbd.o : kbd.c defs.h command.h
+ cc -c kbd.c
+command.o : command.c defs.h command.h
+ cc -c command.c
+display.o : display.c defs.h buffer.h
+ cc -c display.c
+insert.o : insert.c defs.h buffer.h
+ cc -c insert.c
+search.o : search.c defs.h buffer.h
+ cc -c search.c
+files.o : files.c defs.h buffer.h command.h
+ cc -c files.c
+utils.o : utils.c defs.h
+ cc -c utils.c
+clean :
+ rm edit main.o kbd.o command.o display.o \
+ insert.o search.o files.o utils.o
+</pre>
+</div> <p>We split each long line into two lines using backslash/newline; this is like using one long line, but is easier to read. See <a href="splitting-lines">Splitting Long Lines</a>. </p> <p>To use this makefile to create the executable file called <samp>edit</samp>, type: </p> <div class="example"> <pre class="example">make
+</pre>
+</div> <p>To use this makefile to delete the executable file and all the object files from the directory, type: </p> <div class="example"> <pre class="example">make clean
+</pre>
+</div> <p>In the example makefile, the targets include the executable file ‘<samp>edit</samp>’, and the object files ‘<samp>main.o</samp>’ and ‘<samp>kbd.o</samp>’. The prerequisites are files such as ‘<samp>main.c</samp>’ and ‘<samp>defs.h</samp>’. In fact, each ‘<samp>.o</samp>’ file is both a target and a prerequisite. Recipes include ‘<samp>cc <span class="nolinebreak">-c</span> main.c</samp>’ and ‘<samp>cc <span class="nolinebreak">-c</span> kbd.c</samp>’. </p> <p>When a target is a file, it needs to be recompiled or relinked if any of its prerequisites change. In addition, any prerequisites that are themselves automatically generated should be updated first. In this example, <samp>edit</samp> depends on each of the eight object files; the object file <samp>main.o</samp> depends on the source file <samp>main.c</samp> and on the header file <samp>defs.h</samp>. </p> <p>A recipe may follow each line that contains a target and prerequisites. These recipes say how to update the target file. A tab character (or whatever character is specified by the <code>.RECIPEPREFIX</code> variable; see <a href="special-variables">Special Variables</a>) must come at the beginning of every line in the recipe to distinguish recipes from other lines in the makefile. (Bear in mind that <code>make</code> does not know anything about how the recipes work. It is up to you to supply recipes that will update the target file properly. All <code>make</code> does is execute the recipe you have specified when the target file needs to be updated.) </p> <p>The target ‘<samp>clean</samp>’ is not a file, but merely the name of an action. Since you normally do not want to carry out the actions in this rule, ‘<samp>clean</samp>’ is not a prerequisite of any other rule. Consequently, <code>make</code> never does anything with it unless you tell it specifically. Note that this rule not only is not a prerequisite, it also does not have any prerequisites, so the only purpose of the rule is to run the specified recipe. Targets that do not refer to files but are just actions are called <em>phony targets</em>. See <a href="phony-targets">Phony Targets</a>, for information about this kind of target. See <a href="errors">Errors in Recipes</a>, to see how to cause <code>make</code> to ignore errors from <code>rm</code> or any other command. </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/Simple-Makefile.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Simple-Makefile.html</a>
+ </p>
+</div>