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

Viewed 122

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?

1 Answers

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.

Related