1

I'm working on a project using makefile. Some of the make rules have the following format:

a.o b.o c.o : %.o : %.c
    gcc -c $< -o $@

What's the meaning of such a rule? I'm unable to find explanation in the official manual, but I guess it's used for applying pattern-matching on only the *.o files listed as targets. Is that correct?

Jing Li
  • 639
  • 7
  • 18

1 Answers1

2

Quoting from the GNU Make,

Static Pattern Rules.

Static pattern rules are rules which specify multiple targets and construct the prerequisite names for each target based on the target name. They are more general than ordinary rules with multiple targets because the targets do not have to have identical prerequisites. Their prerequisites must be analogous, but not necessarily identical.

The syntax for static pattern rules:

targets : target-pattern: prereq-patterns …
        recipe
        …

Pattern rule is mentioned as % in the target. It matches any target ends with .o here a.o, b.o and c.o.

Here ‘$<’ is the automatic variable that holds the name of the prerequisite and ‘$@’ is the automatic variable that holds the name of the target.

a.o b.o c.o : %.o : %.c
    gcc -c $< -o $@
danglingpointer
  • 4,708
  • 3
  • 24
  • 42