文件
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;
}
主.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 <iostream>
从 foo.hpp 移出并进入 main.cpp:make
,输出为1,文件为48K。将 int = 1
更改为 int = 2
。make
:输出为2,文件为1.9M,几乎全部都在foo.hpp.gch中。将 int = 2
更改为 int = 3
。make
:输出为3,foo.hpp.gch did更新。
问题
为什么
make
在一种情况下会更新预编译头 (.gch),而在另一种情况下则不会?为什么文件大小差异如此之大? iostream
的内容怎么了?如何强制 make
始终考虑头文件中的更改?我尝试根据 this answer将
-fsyntax-only
添加到 CPPFLAGS
,但这给出了错误。