Makefile目标依赖于环境变量中的文件

cd1*_*cd1 2 dependencies makefile environment-variables target

如果我make像这样跑:

make VAR=dir
Run Code Online (Sandbox Code Playgroud)

有没有办法将VAR变量指向的位置添加为目标依赖项?实际上,我需要在该目录中定义一个文件作为依赖项.

我会去:

target: $(VAR)/file.txt
  echo yes
Run Code Online (Sandbox Code Playgroud)

但是如果没有定义变量,目标就会理解/file.txt,这不是我想要的.我还想过创建一个虚假的目标来检查变量,test但是每次都会执行虚假目标,因此target也会这样做.

任何解决方案?

Bet*_*eta 5

如果未定义变量,您还没有说出您想要的行为,但这可能是您想要的:

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

target:
  echo yes
  @echo and here are the dependencies: $^
Run Code Online (Sandbox Code Playgroud)