diff options
Diffstat (limited to 'devdocs/gnu_make/variables-simplify.html')
| -rw-r--r-- | devdocs/gnu_make/variables-simplify.html | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/devdocs/gnu_make/variables-simplify.html b/devdocs/gnu_make/variables-simplify.html new file mode 100644 index 00000000..7ccc54c5 --- /dev/null +++ b/devdocs/gnu_make/variables-simplify.html @@ -0,0 +1,38 @@ + <h1 class="section">Variables Make Makefiles Simpler</h1> <p>In our example, we had to list all the object files twice in the rule for <samp>edit</samp> (repeated here): </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 +</pre> +</div> <p>Such duplication is error-prone; if a new object file is added to the system, we might add it to one list and forget the other. We can eliminate the risk and simplify the makefile by using a variable. <em>Variables</em> allow a text string to be defined once and substituted in multiple places later (see <a href="using-variables">How to Use Variables</a>). </p> <p>It is standard practice for every makefile to have a variable named <code>objects</code>, <code>OBJECTS</code>, <code>objs</code>, <code>OBJS</code>, <code>obj</code>, or <code>OBJ</code> which is a list of all object file names. We would define such a variable <code>objects</code> with a line like this in the makefile: </p> <div class="example"> <pre class="example">objects = main.o kbd.o command.o display.o \ + insert.o search.o files.o utils.o +</pre> +</div> <p>Then, each place we want to put a list of the object file names, we can substitute the variable’s value by writing ‘<samp>$(objects)</samp>’ (see <a href="using-variables">How to Use Variables</a>). </p> <p>Here is how the complete simple makefile looks when you use a variable for the object files: </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 : 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 $(objects) +</pre> +</div><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/Variables-Simplify.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Variables-Simplify.html</a> + </p> +</div> |
