为了在不弄乱相对路径的情况下从不同位置调用我的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
为了从不同位置调用我的Makefile而不弄乱相对路径,我使用另一个答案中给出的Makefile变量引用路径:DIR = $(shell目录名$(realpath $(...
MAKEFILE_LIST
的值在include
之后更改,并且变量DIR
的扩展在使用时发生。为了演示,我在Makefile上撒了info