24 GNU make
you use the value of objects in a recipe, the shell may perform wildcard expansion when
the recipe runs. To set objects to the expansion, instead use:
objects := $(wildcard *.o)
See Section 4.3.3 [Wildcard Function], page 24.
4.3.2 Pitfalls of Using Wildcards
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 foo is made from
all the object files in the directory, and you write this:
objects = *.o
foo : $(objects)
cc -o foo $(CFLAGS) $(objects)
The value of objects is the actual string ‘*.o’. Wildcard expansion happens in the rule
for foo, so that each existing ‘.o’ file becomes a prerequisite of foo and will be recompiled
if necessary.
But what if you delete all the ‘.o’ files? When a wildcard matches no files, it is left as it
is, so then foo will depend on the oddly-named file *.o. Since no such file is likely to exist,
make will give you an error saying it cannot figure out how to make *.o. This is not what
you want!
Actually it is possible to obtain the desired result with wildcard expansion, but you need
more sophisticated techniques, including the wildcard function and string substitution.
These are described in the following section.
Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to separate
directories in pathnames, like so:
c:\foo\bar\baz.c
This is equivalent to the Unix-style c:/foo/bar/baz.c (the c: part is the so-called drive
letter). When make runs on these systems, it supports backslashes as well as the Unix-
style forward slashes in pathnames. However, this support does not include the wildcard
expansion, where backslash is a quote character. Therefore, you must use Unix-style slashes
in these cases.
4.3.3 The Function wildcard
Wildcard expansion happens automatically in rules. But wildcard expansion does not nor-
mally 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 wildcard function, like this:
$(wildcard pattern...)
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 wildcard function.
Note that this is different from how unmatched wildcards behave in rules, where they are
used verbatim rather than ignored (see Section 4.3.2 [Wildcard Pitfall], page 24).
One use of the wildcard function is to get a list of all the C source files in a directory,
like this:
Comentarios a estos manuales