What is a .h.gch file?
C++G++C++ Problem Overview
I recently had a class project where I had to make a program with G++.
I used a makefile and for some reason it occasionally left a .h.gch file behind.
Sometimes, this didn't affect the compilation, but every so often it would result in the compiler issuing an error for an issue which had been fixed or which did not make sense.
I have two questions:
-
What is a .h.gch file and what is one used for? and
-
Why would it cause such problems when it wasn't cleaned up?
C++ Solutions
Solution 1 - C++
A .gch
file is a precompiled header.
If a .gch
is not found then the normal header files will be used.
However, if your project is set to generate pre-compiled headers it will make them if they don’t exist and use them in the next build.
Sometimes the *.h.gch
will get corrupted or contain outdated information, so deleting that file and compiling it again should fix it.
Solution 2 - C++
If you want to know about a file, simply type on terminal
file filename
file a.h.gch
gives:
GCC precompiled header (version 013) for C
Solution 3 - C++
Its a GCC precompiled header.
Wikipedia has a half decent explanation, http://en.wikipedia.org/wiki/Precompiled_header
Solution 4 - C++
Other answers are completely accurate with regard to what a gch file is. However, context (in this case, a beginner using g++) is everything. In this context, there are two rules:
-
Never, ever, ever put a .h file on a g++ compile line. Only .cpp files. If a .h file is ever compiled accidentally, remove any *.gch files
-
Never, ever, ever put a .cpp file in an #include statement.
If rule one is broken, at some point the problem described in the question will occur. If rule two is broken, at some point the linker will complain about multiply-defined symbols.
Solution 5 - C++
a) They're precompiled headers: http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html
b) They contain "cached" information from .h files and should be updated every time you change respective .h file. If it doesn't happen - you have wrong dependencies set in your project