为什么在我的Makefile中包含文件会更改我的Makefile目录变量?

问题描述 投票:0回答:1

为了在不弄乱相对路径的情况下从不同位置调用我的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 gnu-make
1个回答
0
投票
这是因为MAKEFILE_LIST的值在include之后更改,并且变量DIR的扩展在使用时发生。

为了演示,我在Makefile上撒了info

© www.soinside.com 2019 - 2024. All rights reserved.