【问题标题】:How to know if compiler is taking advantage of the pch.h.gch file?如何知道编译器是否正在利用 pch.h.gch 文件?
【发布时间】:2022-01-05 13:24:27
【问题描述】:

有没有办法检查 GCC 是否使用预编译头文件?

另外,我生成pch.h.gch 文件如下:

g++ -std=c++20 -Wall -O3 -flto pch.h -o pch.h.gch

但是生成的文件总是被命名为pch.h 并且没有.gch 扩展名。为什么会这样?它用于自动添加扩展名。但现在不行了。

编辑:另一个问题是,是否有必要在预编译的头文件中添加包含保护(例如#pragma once)?

【问题讨论】:

  • @KamilCuk 我没有在我的pch.h 中使用任何警卫。 GCC 没有抱怨。使用时,GCC会给出警告。
  • Edit: Another question is that, 每个问题请回答一个问题。参见例如meta.stackoverflow.com/questions/266767/…

标签: c++ gcc gnu-make precompiled-headers


【解决方案1】:

问题是:

如何知道编译器是否在利用 pch.h.gch 文件?

使用以下源文件:

==> f.hpp <==
static inline int f() { return 1; }

==> main.cpp <==
#include "f.hpp"
int main() {
    return f();
}

我们可以检查gcc进行的系统调用,看看它是否打开了预编译的头文件:

$ gcc f.hpp
$ strace -e openat -ff gcc -c main.cpp 2>&1 | grep 'f\.hpp\.gch'
[pid 877340] openat(AT_FDCWD, "f.hpp.gch", O_RDONLY|O_NOCTTY) = 4

我们在上面看到gcc 打开,因此我们假设gcc 使用f.hpp.gch

【讨论】:

  • 这是在 Linux 上吗?这些选项在 Windows 上有等效项吗?
  • Is this on Linux? 是的,strace 是一个 linux 工具。 Do these options have equivalents on Windows? 不知道,我不在 Windows 上工作。 Gcc 无论如何都不可能在 Windows 上工作(没有一些 linux 兼容层)。
  • 我使用 mingw64。效果很好。
猜你喜欢
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多