145

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?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
mnuzzo
  • 3,529
  • 4
  • 26
  • 29
  • 21
    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 06 '09 at 21:13
  • 6
    Outside the **GCC** world, **.gch** files are called **.pch**. – Patapoom Aug 02 '17 at 13:48

5 Answers5

140

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.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Dunewalker
  • 1,554
  • 1
  • 9
  • 4
52

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
BЈовић
  • 62,405
  • 41
  • 173
  • 273
riteshkasat
  • 803
  • 10
  • 15
23

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.

tgibson
  • 341
  • 2
  • 4
  • 2
    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 06:13
  • In your rule 2, we #include .cpp files if they have definition of template function – NAND May 09 '20 at 22:18
  • given rule 1) how does one create the .gch file? – Frank Puck Dec 03 '21 at 15:16
  • @FrankPuck, if one is a beginner learning C++ and g++ (the OP's situation), one does not create a .gch file. My answer doesn't apply to a mature software engineer who has cause to compile a header file. – tgibson Dec 04 '21 at 22:59
22

Its a GCC precompiled header.

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

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
Goz
  • 61,365
  • 24
  • 124
  • 204
17

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