views:

16

answers:

1

hi,

if I run make like this:

make VAR=dir

is there a way to add the location pointed by the VAR variable as a target dependency? actually, I need to define a file inside that directory as a dependency.

I'd go with:

target: $(VAR)/file.txt
  echo yes

but if the variable was not defined, the target will understand /file.txt, which is not what I want. I also thought about creating a phony target to check for the variable, with test, but then the phony target would be executed every time and, consequently, target also would.

any solution to that?

+1  A: 

You haven't said what behavior you want if the variable is not defined, but this is probably what you want:

ifdef VAR
target: $(VAR)/file.txt
endif

target:
  echo yes
  @echo and here are the dependencies: $^
Beta
thanks, that was what I needed. actually, I'd use an `else` clause to enclose the last target so make won't complain about redefining a target.
cd1
Make won't complain, and the trick won't work if you put the second part in an `else` clause. Try it.
Beta