k1r1t0
k1r1t0

Reputation: 767

Reassign Makefile variable for target

This is a simple Makefile:

    ...
VAR    := 
FLAGS  := --flag=$(VAR)

target_1: VAR = 1
target_1: $(DEPS_1)
    $(CMD) $(FLAGS)
    ...

target_2: VAR = 2
target_2: $(DEPS_2)
    $(CMD) $(FLAGS)
    ...

I want to reassign variable VAR to make make recalculate value of FLAGS to use it for different targets, is there a way to do something like this?

Upvotes: 1

Views: 56

Answers (1)

MadScientist
MadScientist

Reputation: 100836

Don't use simple expansion for FLAGS:

FLAGS  := --flag=$(VAR)

is wrong; you want this:

FLAGS  = --flag=$(VAR)

If you expand FLAGS immediately, the all the variables are expanded to whatever their value is at that point in the makefile. There is no opportunity to redo that later, because the variable reference is gone: it's replaced with the value.

Upvotes: 2

Related Questions