Mikolaj
Mikolaj

Reputation: 140

Makefile target global variable

I would like to have something like that:

PrintTarget:
    @echo Building $(TARGET)

SetRelTarget:   TARGET = Release
SetRelTarget:
    @echo Target is set.

BuildRel: SetRelTarget PrintTarget

But TARGET variable set in SetRelTargetAs is not a global. My question is:

Is it possible to modify global variables inside the rule and use this modified value outside this rule?

Thank you.

Upvotes: 1

Views: 1680

Answers (2)

Eric
Eric

Reputation: 471

Set TARGET to release in BuildRel, then it should apply to all its prerequisites (although it's not global),

PrintTarget:
        @echo Building $(TARGET)

#SetRelTarget:   TARGET = Release
SetRelTarget:
        @echo Target is set to $(TARGET).

BuildRel: TARGET = Release
BuildRel: SetRelTarget PrintTarget

Upvotes: 1

l0b0
l0b0

Reputation: 58858

Since there is nothing variable in the code you can simply do this:

TARGET = Release

PrintTarget:
    @echo Building $(TARGET)

BuildRel: PrintTarget

Demo:

$ make BuildRel
Building Release
$ make TARGET=foo BuildRel
Building foo

If your use case is more complex please include more details.

Upvotes: 0

Related Questions