当我删除 vpath 指令时,为什么这个 makefile 会失败?

Why does this makefile fails when I remove the vpath directive?

我正在研究 GNU GCC makefile,并制作了这个简单的文件来构建 "Hello, world!" 程序。

CC=gcc
SOURCES=./Source/main.c
BUILD_DIR=./Build
TARGET=main

all: $(BUILD_DIR)/$(TARGET).exe

OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(SOURCES)))

$(BUILD_DIR)/%.o: %.c
    $(CC) -c $< -o $@

$(BUILD_DIR)/%.exe: $(OBJECTS)
    $(CC) $< -o $@

我不明白为什么当我删除或评论这一行时 vpath %.c $(sort $(dir $(SOURCES))),停止并 returns 此错误:


> Executing task: D:\Servers\Compilers\MinGW\bin\mingw32-make.exe GCC_PATH=D:\Servers\Compilers\gcc-arm-none-eabi\bin <

mingw32-make: *** No rule to make target 'Build/main.exe', needed by 'all'.  Stop.

你能解释一下为什么吗?

您向目标文件添加了前缀,因此您的 OBJECTS 是“./Build/main.o”。
但是当你使用静态规则时,'.o' 后缀将被 '.c' 替换,所以你的对象是 ./Build/main.c.Your main.c 不在目录 "Build",所以肯定是failed.When你加上vpath %.c $(sort $(dir $(SOURCES))),它会自动从目录Build.

中找到.c文件