0
votes

I need pointing what I'm missing about makefiles. For given makefile:

VPATH=
targ1: VPATH=src
targ1: targ2

targ2: targ2
    echo $(VPATH)

and empty src direcotry, every time calling "make targ1" I get as expected:

echo src
src

since there is not file targ2 in makefile directory and in src direcotry targ2 needs to be updated.

But the problem is that even when I create targ2 file in src directory targ2 is updated every time of calling 'make targ1'. In that case VPATH is not working as expected.

The value of the make variable VPATH specifies a list of directories that make should search. Most often, the directories are expected to contain prerequisite files that are not in the current directory; however, make uses VPATH as a search list for both prerequisites and targets of rules.

Howewer problem does not occure when I set VPATH in global scope:

VPATH=src
targ1: VPATH=src
targ1: targ2

targ2: targ2
    echo $(VPATH)

Result is: make: Nothing to be done for 'targ1'.

Why there is such difference?

1

1 Answers

0
votes

As with automatic variables, [target-specific] values are only available within the context of a target’s recipe...

So in your first example, the value you gave to VPATH is not available to Make to find the prerequisite targ2.