0

Suppose I want a program (say, Some_Command) to obtain the name of the target as one of its parameter (passed with flag -p) from the command-line, and that I want the parameters with which the command is called to be contained in a single variable (say, COMMON_ARG) that I can re-use. I know I can access the target's name using $@, but I believe this variable has a value only from inside the rule. Is there any way to defer the evaluation of that variable?

Typically, I would like to have

COMMON_ARG:= "-p $@"
foo:bar
    Some_Command $(COMMAND_ARG) $< -o $@

so that Make bar calls

Some_Command -p foo bar -o foo
2
  • Possibly related: stackoverflow.com/q/2403881 – Clément May 17 at 19:11
  • 1
    The := operator is completely non-portable. It has been introduced in 1986 by SunPro Make as conditional macro assignment. GNU make much later introduced := for something different that is not needed in a standard compliant make, since you always can prevent expansion on use by typing $$(NAME). – schily May 24 at 11:29
3

If you declare a variable with = (instead of :=), it will be recursively expanded wherever it’s used, and not when it’s declared. Thus

COMMON_ARG = -p $@
foo: bar
        Some_Command $(COMMAND_ARG) $< -o $@

will expand $@ inside the recipe, giving the effect you’re after.

1
  • Thanks a lot. That's what I thought I've tried, but as it turns out, COMMON_ARG was used in a second variable that was using := instead of =. So all the variables that relates to this one have to not be recursively expanded. Thanks a lot for pointing that out, it greatly helped me. – Clément May 18 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.