【发布时间】:2017-03-29 22:03:05
【问题描述】:
我在让预编译的头文件工作时遇到了麻烦,所以我想出了以下最小工作示例。
这是头文件foo.h
#include <iostream>
using namespace std;
void hello() {
cout << "Hello World" << endl;
}
我将其编译为g++ -c foo.h,给了我一个编译后的标题foo.gch。我希望当我编译以下包含foo.h 的源文件时,它应该选择标题foo.h.gch,我很好。
// test.cpp
#include <cstdio> // Swap ordering later
#include "foo.h" // ------------------
int main() {
hello();
}
但令人惊讶的是,这并没有使用foo.h.gch 编译,而是使用foo.h。要验证您可以将其编译为g++ -H test.cpp
但是,如果我将包含的头文件的顺序更改如下:
// test.cpp
#include "foo.h" // ------------------
#include <cstdio> // Ordering swapped
int main() {
hello();
}
现在如果我使用g++ -H test.cpp 编译,它会从foo.h.gch 编译,哇!
所以我想知道这是否是 GCC 中的错误,还是我们应该使用这样的预编译头文件?无论哪种情况,我认为了解它很有用..
【问题讨论】:
标签: c++ gcc precompiled-headers