Contents | < Browse | Browse >
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 dependency of `foo' and will be recompiled if necessary.
But what if you delete all the `.o' files? Then `*.o' will expand
into *nothing*. The target `foo' will have no dependencies and would
be remade by linking no object files. 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. Wildcard Function