summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/wildcard-pitfall.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/wildcard-pitfall.html
new repository
Diffstat (limited to 'devdocs/gnu_make/wildcard-pitfall.html')
-rw-r--r--devdocs/gnu_make/wildcard-pitfall.html13
1 files changed, 13 insertions, 0 deletions
diff --git a/devdocs/gnu_make/wildcard-pitfall.html b/devdocs/gnu_make/wildcard-pitfall.html
new file mode 100644
index 00000000..eb0cb2b9
--- /dev/null
+++ b/devdocs/gnu_make/wildcard-pitfall.html
@@ -0,0 +1,13 @@
+ <h1 class="subsection">Pitfalls of Using Wildcards</h1> <p>Now here is an example of a naive way of using wildcard expansion, that does not do what you would intend. Suppose you would like to say that the executable file <samp>foo</samp> is made from all the object files in the directory, and you write this: </p> <div class="example"> <pre class="example">objects = *.o
+
+foo : $(objects)
+ cc -o foo $(CFLAGS) $(objects)
+</pre>
+</div> <p>The value of <code>objects</code> is the actual string ‘<samp>*.o</samp>’. Wildcard expansion happens in the rule for <samp>foo</samp>, so that each <em>existing</em> ‘<samp>.o</samp>’ file becomes a prerequisite of <samp>foo</samp> and will be recompiled if necessary. </p> <p>But what if you delete all the ‘<samp>.o</samp>’ files? When a wildcard matches no files, it is left as it is, so then <samp>foo</samp> will depend on the oddly-named file <samp>*.o</samp>. Since no such file is likely to exist, <code>make</code> will give you an error saying it cannot figure out how to make <samp>*.o</samp>. This is not what you want! </p> <p>Actually it is possible to obtain the desired result with wildcard expansion, but you need more sophisticated techniques, including the <code>wildcard</code> function and string substitution. See <a href="wildcard-function">The Function <code>wildcard</code></a>. </p> <p>Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to separate directories in pathnames, like so: </p> <div class="example"> <pre class="example"> c:\foo\bar\baz.c
+</pre>
+</div> <p>This is equivalent to the Unix-style <samp>c:/foo/bar/baz.c</samp> (the <samp>c:</samp> part is the so-called drive letter). When <code>make</code> runs on these systems, it supports backslashes as well as the Unix-style forward slashes in pathnames. However, this support does <em>not</em> include the wildcard expansion, where backslash is a quote character. Therefore, you <em>must</em> use Unix-style slashes in these cases. </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/Wildcard-Pitfall.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Wildcard-Pitfall.html</a>
+ </p>
+</div>