summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/wildcard-function.html
blob: 469917fb33496c04d86a8e72049edcdeddc730a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  <h1 class="subsection">The Function wildcard</h1>  <p>Wildcard expansion happens automatically in rules. But wildcard expansion does not normally take place when a variable is set, or inside the arguments of a function. If you want to do wildcard expansion in such places, you need to use the <code>wildcard</code> function, like this: </p> <div class="example"> <pre class="example">$(wildcard <var>pattern</var>…)
</pre>
</div> <p>This string, used anywhere in a makefile, is replaced by a space-separated list of names of existing files that match one of the given file name patterns. If no existing file name matches a pattern, then that pattern is omitted from the output of the <code>wildcard</code> function. Note that this is different from how unmatched wildcards behave in rules, where they are used verbatim rather than ignored (see <a href="wildcard-pitfall">Wildcard Pitfall</a>). </p> <p>As with wildcard expansion in rules, the results of the <code>wildcard</code> function are sorted. But again, each individual expression is sorted separately, so ‘<samp>$(wildcard *.c *.h)</samp>’ will expand to all files matching ‘<samp>.c</samp>’, sorted, followed by all files matching ‘<samp>.h</samp>’, sorted. </p> <p>One use of the <code>wildcard</code> function is to get a list of all the C source files in a directory, like this: </p> <div class="example"> <pre class="example">$(wildcard *.c)
</pre>
</div> <p>We can change the list of C source files into a list of object files by replacing the ‘<samp>.c</samp>’ suffix with ‘<samp>.o</samp>’ in the result, like this: </p> <div class="example"> <pre class="example">$(patsubst %.c,%.o,$(wildcard *.c))
</pre>
</div> <p>(Here we have used another function, <code>patsubst</code>. See <a href="text-functions">Functions for String Substitution and Analysis</a>.) </p> <p>Thus, a makefile to compile all C source files in the directory and then link them together could be written as follows: </p> <div class="example"> <pre class="example">objects := $(patsubst %.c,%.o,$(wildcard *.c))

foo : $(objects)
        cc -o foo $(objects)
</pre>
</div> <p>(This takes advantage of the implicit rule for compiling C programs, so there is no need to write explicit rules for compiling the files. See <a href="flavors">The Two Flavors of Variables</a>, for an explanation of ‘<samp>:=</samp>’, which is a variant of ‘<samp>=</samp>’.) </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-Function.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Wildcard-Function.html</a>
  </p>
</div>