Question

Suppose you have 2 files:

  • A.h and
  • A.cpp, where A.cpp includes A.h

You precompile your header to A.h.gch and delete A.h

Having 2 files:

  • A.cpp and
  • A.h.gch,

How do you compile a final product, using g++?

In my case, g++ is complaining that .h file is nowhere to be found.

Was it helpful?

Solution

I'm not expert, but from Wikipedia:

GCC can only use the precompiled version if the same compiler switches are set as when the header was compiled and it may use at most one.

I've tried:

g++ a.h
rm a.h
g++ -c a.cpp

you can see if g++ is using it with:

g++ -H -c a.cpp

! a.h.gch
a.cpp

OTHER TIPS

I assumed that g++ would check the date of the header, to see if the gch is outdated. To my surprise, this is not the case. I could modify and rename the header, and could still compile the cpp.

Compilation fails if certain parameters are different than when the gch has been created. Therefore I'd assume that your compilation fails because at least one of the conditions mentioned in the GCC precompiled header docs is not met.

If you often switch between different compile configurations, you can use a directory containing a pch for each configuration, as described in that document.

If you just want to make sure that/if the pch is used, use the -Winvalid-pch option, and make sure to update the pch after each change to the header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top