3

What does it mean to have a rule specified as follows?

foo: bar : baz

I understand that foo is a target, bar and baz are prerequisites but why there is another colon between bar and baz - what's the meaning of that colon?

name
  • 5,095
  • 5
  • 27
  • 36
  • Does it really say `:` there, and not `|`? If so, which make dialect is this? GNU make rejects it. –  Feb 10 '15 at 11:42
  • @hvd GNU Make 3.82 Built for x86_64-redhat-linux-gnu – name Feb 10 '15 at 11:44
  • Huh. I tried it with "GNU Make 4.0 Built for x86_64-pc-linux-gnu", and I get "Makefile:1: *** target pattern contains no '%'. Stop." –  Feb 10 '15 at 11:48

2 Answers2

4

The feature you are thinking of is a static pattern rule, and the syntax you give: foo : bar : baz is illegal; the bar part must be a pattern (that is, it must contain a % character).

Tripleee gives a reasonable explanation, except the statement when we build foo, bar depends on baz is somewhat confusing. What a static pattern rule means is that for each word in targets, create a new explicit rule where the target is the result of applying the pattern pattern to that word with the prerequisites prerequisites.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
1

The general syntax is

targets: pattern: prerequisites

So this -- grossly oversimplified -- example of yours says, basically, when we build foo, bar depends on baz. However, the second argument needs to be a pattern rule, so your example is in fact a syntax error.

A more useful and correct example would be along the lines of

$(OBJS): %.o: ick.h

which says that if you are building one of OBJS, their .o file depends on ick.h.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    _when we build `foo`, `bar` depends on `baz`_ is not right. The important thing to realize is that the question is totally wrong: as hvd says above it's a syntax error to use `foo : bar : baz`. The `bar` word _must_ be a _pattern_, and the pattern _must_ match each word in the `foo` section. What this means is, "for each word in the `foo` section, create a new rule where the target is the result of applying the pattern `bar` to each word and the prerequisites are `baz`". – MadScientist Feb 10 '15 at 13:26
  • @MadScientist Please move your comment to a new answer. – name Feb 10 '15 at 14:41