【发布时间】:2020-03-01 17:25:26
【问题描述】:
为了从不同位置调用我的 Makefile 而不会弄乱相对路径,我使用 another answer 中给出的 Makefile 变量引用路径:
DIR=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
当我包含其他文件时,我知道MAKEFILE_LIST 会有所不同,但由于我在进行任何包含之前将其值存储在变量中,所以我很惊讶变量值不同。
例子:
$ tree .
.
├── another_file
└── subdirectory
└── Makefile
$ cat Makefile
DIR=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
test:
@echo $(DIR)
#include $(DIR)/../another_file
$ make
/subdirectory
正如预期的那样。但是如果我取消注释包含行,我会得到
$ make
/
这对我来说没有意义,因为 another_file 仍然包含在内,没有错误表明 $(DIR) 的值是 /subdirectory。
注意make目标放在include语句之前,切换顺序时行为不会改变。猜猜这是由于预处理造成的,但它仍然没有向我解释为什么 $(DIR) 似乎有不同的值。
$ make --version
GNU Make 3.81
...
This program built for i386-apple-darwin11.3.0
【问题讨论】: