【问题标题】:Why does make update precompiled headers sometimes, but not always?为什么 make 有时会更新预编译的头文件,但并非总是如此?
【发布时间】:2020-06-11 09:09:58
【问题描述】:

文件

foo.hpp

#include <iostream>
class foo
{
public:
    foo(int = 1);
    int get_x() const { return x; }
private:
    int x;
};

foo.cpp

#include "foo.hpp"
foo::foo(int xp)
{
    x = xp;
}

main.cpp

#include "foo.hpp"
int main()
{
    foo bar;
    std::cout << bar.get_x() << std::endl;
}

Makefile(对于这样一个简单的例子来说这有点过头了,但我想使用这样的东西)

SHELL = /bin/bash
CC = g++
CPPFLAGS = -MMD -MP
SRC = $(wildcard *.cpp)
OBJ = $(SRC:.cpp=.o)
EXECUTABLE = main

all: $(SRC) $(EXECUTABLE)

$(EXECUTABLE): $(OBJ)
    $(CC) $(OBJ) -o $@

.cpp.o:
    $(CC) $(CPPFLAGS) -c $^

-include $(SRC:.cpp=.d)

观察到的行为
文件大小为 20K。运行make,输出为1,文件为48K。现在将头文件中的默认参数int = 1 更改为int = 2
make:输出为2,文件为11M。几乎所有这些都在 foo.hpp.gch 中。将int = 2 更改为int = 3
make:输出仍然是2,foo.hpp.gch 没有更新。

现在,如果我将 #include &lt;iostream&gt; 从 foo.hpp 移到 main.cpp:
文件为 20K。运行make,输出为1,文件为48K。将int = 1 更改为int = 2
make:输出为 2,文件为 1.9M,几乎所有这些都在 foo.hpp.gch 中。将 int = 2 更改为 int = 3
make:输出为 3,foo.hpp.gch 已更新

问题
为什么make 在一种情况下更新预编译头(.gch),而在另一种情况下不更新?为什么文件大小如此不同? iostream的内容怎么了?以及如何强制make 始终考虑头文件的更改?我尝试按照this answer-fsyntax-only 添加到CPPFLAGS,但这给出了错误。

【问题讨论】:

  • 生成预编译头的规则在哪里?
  • @MaximEgorushkin Makefile 完全如图所示。
  • gcc docs at gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html 列出了一系列影响预编译头文件使用的限制,包括它们的内容或大小可能不同的几个原因。

标签: c++ makefile dependencies gnu-make precompiled-headers


【解决方案1】:

为了部分回答我自己的问题,我在 Makefile 中将 $^ 更改为 -o $@ $&lt;,这似乎可以解决所有问题。现在头文件每次更新,合并后的文件只有144K。我有这个想法here

【讨论】:

    猜你喜欢
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多