【问题标题】:Makefile only tracks the first fileMakefile 只跟踪第一个文件
【发布时间】:2018-07-18 13:18:23
【问题描述】:

我的 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 只跟踪第一个文件。我想知道哪一部分出了问题。

【问题讨论】:

  • 您可能希望在这些对象上加上 -c 标志,因为它们只是构建对象而不是链接。

标签: c++ makefile


【解决方案1】:

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

您可以运行 make test1.o test2.o 来专门制作两者,或者添加一个假目标,这样它就会默认制作 all - 这是许多 makefile 的内容 使用。

使用 .PHONY 告诉 Make all 不是一个真实的文件,所以它不会出现异常,以防万一你有一个名为 all 的文件。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      相关资源
      最近更新 更多