0

Here is the makefile.

.PHONY: all target1 target2 target all: target1 target2 target1: NUM = 1 target2: NUM = 2 target1 target2: target target: @echo "this is target ${NUM}" 

And the output is:

this is target 1 

Why is only the first prerequisite executed ? I guess it's related to the variable. Thanks!

0

1 Answer 1

4

Your Makefile says the following:

  • all targets are phony (i.e. they don’t correspond to on-disk artifacts);
  • all depends on target1 and target2, and since it’s the first target, is the default target;
  • target1 sets NUM to 1 (using target-specific variable assignments);
  • target2 sets NUM to 2;
  • target1 and target2 both depend on target;
  • target must be satisfied by running the given echo command.

Running make with these definitions will result in the following:

  • all isn’t satisfied, since it’s phony, and needs target1 and target2;
  • target1 isn’t satisfied, since it’s phony, and needs target (with NUM set to 1);
  • target isn’t satisfied, since it’s phony, and has no prerequisites, so make runs the echo command;
  • target2 isn’t satisfied, since it’s phony, but target is satisfied, since it’s been processed already, so nothing happens.

You can’t use targets as functions, as you’re trying to do here; some Make implementations do support functions, but you don’t need that:

target1 target2: @echo "this is target ${NUM}" 
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.