Home > Enterprise >  What does ` $([email protected]) ` mean in a Makefile when it appears in the prerequisites of a rule?
What does ` $([email protected]) ` mean in a Makefile when it appears in the prerequisites of a rule?

Time:03-15

TARGETS = a b c d
$(TARGETS) : $([email protected])  
    $(CC)  -c [email protected]
    $(CC) [email protected] -o ../bin/$@
    rm -f [email protected]

I know how it works, this rule iterates over the variable TARGETS to compile the .c file with filename identical to its corresponding part in TARGETS. But I was not capable of finding the detail of this grammar in the mannul.

Which part should I refer to to know more about it?

CodePudding user response:

$@ only has a value within the recipe, so the obvious:

$(TARGETS) : [email protected]
     ...

doesn't work, because when the rule is read, $@ has no value. It's explicitly called out in https://www.gnu.org/software/make/manual/make.html#Automatic-Variables

However, if you enable secondary expansion, which applies to rules after the special target

.SECONDEXPANSION:

then $([email protected]) is expanded to [email protected] when the rule is first read, and when the rule is used, $@ will have a value (of the target being built) thanks to secondary expansion. See: https://www.gnu.org/software/make/manual/make.html#Secondary-Expansion

Incidentally, I'm not sure why the rule is so complicated. I don't see why it needs to build the .o file only to delete it later, when C compilers can build a binary from source files without generating an intermediate .o file.

$(TARGETS) : $([email protected])
    $(CC) [email protected] -o ../bin/$@

And the rule is faulty anyway, because the target of the rule is not generated by the recipe: the target is for example "a", but the file generated is "../bin/a".

CodePudding user response:

It just evaluates to an empty string and has no effect. Sorry to bother you.

  • Related