Makefile仅跟踪第一个文件

Makefile only tracks the first file

本文关键字:文件 第一个 跟踪 Makefile      更新时间:2023-10-16

我的makefile中有两个.cpp文件:

test1.o : test1.cpp
        g++ test1.cpp -o test1.o
test2.o : test2.cpp
        g++ test2.cpp -o test2.o

在我对test1.cpp和test2.cpp进行更改后,在命令行中键入 make,只重新编译了 test1.o,而对test2.o没有任何作用。

但是,如果我在makefile中交换两个块,test2.o将自动重新编译,并且test1.o不会。

似乎我的makefile只能跟踪第一个文件。我想知道哪一部分出错。

默认情况下make将在makefile中构建第一件事,如果您不告诉它要构建什么。

您可以运行make test1.o test2.o以专门制作,也可以添加假目标,因此默认情况下将使all - 这就是许多Makefiles使用。

使用.PHONY告诉MAKE all不是一个真实的文件,因此,如果您出于某种原因有一个名为 all的文件,则不会出现不当。

all: test1.o test2.o
.PHONY: all

尝试添加默认目标。一个非常简单的起点:all: test1.o test2.o