summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/wildcard-function.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-function.html
new repository
Diffstat (limited to 'devdocs/gnu_make/wildcard-function.html')
-rw-r--r--devdocs/gnu_make/wildcard-function.html17
1 files changed, 17 insertions, 0 deletions
diff --git a/devdocs/gnu_make/wildcard-function.html b/devdocs/gnu_make/wildcard-function.html
new file mode 100644
index 00000000..469917fb
--- /dev/null
+++ b/devdocs/gnu_make/wildcard-function.html
@@ -0,0 +1,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>