Michael Schubert
Michael Schubert

Reputation: 2796

Makefiles: mixed file/directory patterns

I have a Makefile and the following directory structure:

mkdir a
echo 1 > a/b

And the rules (this creates a file b from a/b, and it works fine):

all: ./b

./b: a/b
   echo $^ > $@

When I want to replace /b by a %-pattern, it stops working ("no rule"):

all: ./b

.%: a%
    echo $^ > $@

Why?

./%: a/%  # works fine

Is there some rule that a pattern that corresponds to files can not be used for directories and vice versa?

Upvotes: 0

Views: 50

Answers (1)

Eric
Eric

Reputation: 471

Please check How Patterns Match,

When the target pattern does not contain a slash (and it usually does not), directory names in the file names are removed from the file name before it is compared with the target prefix and suffix. After the comparison of the file name to the target pattern, the directory names, along with the slash that ends them, are added on to the prerequisite file names generated from the pattern rule’s prerequisite patterns and the file name.

So for the failure case, ".%" means to match any target's filename starting with ".".

Upvotes: 1

Related Questions