Makefile 中的第一个目标是隐含的虚假目标吗?

Is the first target in Makefile an implicit phony target?

我正在研究从编译器课程项目中获得的 Makefile。这里只贴出一部分。

# Retain intermediate bitcode files
.PRECIOUS: %.bc

# The default target builds the plugin
plugin:
    make -C lib/p1

# create .bc from source
%.bc:   %.c
    clang -emit-llvm -O0 -c $*.c -o $*.bc

# run printCode on a .bc file
%.printCode: %.bc plugin
    opt -load Debug/lib/P1.so -printCode $*.bc 

如您所见,目标 'plugin' 没有任何依赖关系,如果我理解正确的话,这应该意味着它的配方永远不会运行(除非它被声明为虚假目标,但这里不是这种情况)

但是,当我键入 'make printCode' 时,(printCode 是列表中的最后一个目标)插件目标会执行。这怎么可能?是否有一些隐含的规则表明 Makefile 的第一个目标被视为虚假目标,例如 'all'?

你有点落后了。

plugin 规则这样的规则可以 运行。您可以通过执行 'make plugin' 来 运行,如果它是默认目标(在本例中是第一个目标),则 'make', 或者如果它是另一个必须构建的目标的先决条件。

我不确定当您 'make printCode' 时到底发生了什么,因为您只向我们展示了 makefile 的一部分并且没有适合的规则,但根据这条规则判断:

%.printCode: %.bc plugin
    opt -load Debug/lib/P1.so -printCode $*.bc 

我猜想 printCode 规则取决于 plugin 或类似 foo.printCode 的规则取决于 plugin。所以 Make 看到 plugin 是先决条件,看到不存在这样的文件,因此确定必须构建 plugin。然后它会寻找一个规则来构建 plugin,找到它并 运行s 它。