2

Is there a way to check to see if GCC uses the precompiled header or not?

Also, I generate pch.h.gch file like this:

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

But the generated file is always named as pch.h and without the .gch extension. Why is this happening? It used to automatically add the extension. But now it doesn't.

Edit: Another question is that, is it necessary to add an include guard (e.g. #pragma once) to the precompiled header?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • @KamilCuk I haven't used any guards in my `pch.h`. GCC doesn't complain. When using it, GCC gives a warning. – digito_evo Nov 28 '21 at 09:49
  • `Edit: Another question is that,` Please one question per question. See for example https://meta.stackoverflow.com/questions/266767/what-is-the-the-best-way-to-ask-follow-up-questions . – KamilCuk Nov 28 '21 at 10:02

1 Answers1

2

The question is:

How to know if compiler is taking advantage of the pch.h.gch file?

With the following source files:

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

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

We can inspect system calls made by gcc to see if it opens the precompiled header:

$ 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

We see above that gcc opens, so we assume based on that that gcc uses f.hpp.gch.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Is this on Linux? Do these options have equivalents on Windows? – digito_evo Nov 28 '21 at 09:53
  • 1
    `Is this on Linux?` Yes, [strace](https://en.wikipedia.org/wiki/Strace) is a linux tool. `Do these options have equivalents on Windows?` No idea, I do not work on windows. Gcc is not possible to work on windows (without some linux compatibility layer) anyway. – KamilCuk Nov 28 '21 at 10:00
  • I use mingw64. It works well. – digito_evo Nov 28 '21 at 10:08