summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/wildcard-pitfall.html
blob: eb0cb2b9fba4b16a7d298ea56eb85c5e102b6caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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>