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:

1) What is a .h.gch file and what is one used for? and

2) Why would it cause such problems when it wasn't cleaned up?

Thanks for the help.

  • 10
    gcc creates them if you accidentally tell it to compile a .h file. Don't do that :) (unless you actually want to create a precompiled header) – jalf Aug 6 '09 at 21:13
  • 2
    Outside the GCC world, .gch files are called .pch. – Patapoom Aug 2 '17 at 13:48
up vote 96 down vote accepted

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.

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

Its a GCC precompiled header.

Wikipedia has a half decent explanation, http://en.wikipedia.org/wiki/Precompiled_header

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

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:

  1. 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

  2. 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.

  • Could you detail more on the first point? – Darktega Oct 19 '16 at 4:58
  • 1
    For the 1st point I'll use a function as an example. Before a programmer can call myfunc(), the programmer must describe myfunc() to the compiler using a function prototype. If several different .cpp files call myfunc(), then the prototype must be provided in each .cpp. It is error prone to type out the prototype in each .cpp file. Therefore, the function prototype is placed in a .h file which is #included in each .cpp file where myfunc is called. Header files only contain information for the compiler, not code that is executed. So it is meaningless to put a .h file on a g++ line. – tgibson Oct 20 '16 at 6:13

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.