What Is a .H.Gch File

What is a .h.gch file?

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.

How to make GCC not generate .h.gch files

Files ending in .gch are precompiled headers - header files which have been pre-compiled in order to reduce compilation time when you (re)compile your main program.

They are produced if you invoke the compiler with the header file itself as the target, ie:

gcc myheader.h

Normally you would only be calling the compiler with .c files as targets.

If you don't want it to produce precompiled headers then don't invoke the compiler with the header files as the target.

If you are not deliberately calling the compiler with the headers as targets, you might be using a makefile which is setup to produce these files - it will have rules in it to produce .gch files from .h files. You will need to remove these rules and adjust other rules not to depend on them.

How to use .h.gch files

Using GCH (Gnu preCompiled Headers) is easy, at least in theory.

How to create a GCH

Just use gcc <compiler-options> myfile.h. That will create myfile.h.gch. You can use the -o <name> to specify the name of the output file.

If your header file is C++, you will have to name it myfile.hpp or myfile.hh, or GCC will try to compile it as C and probably fail. Alternatively you can use the -x c++-header compiler option.

How to use a GCH

Put your myfile.h.gch in the same directory than myfile.h. Then it will be used automatically instead of myfile.h, at least as long as the compiler options are the same.

If you do not want to (or can) to put your *.gch in the same directory of your *.h, you can move them to a directory, say ./gch, and add the option -Igch to your compiler command.

How to know your GCH is being used

Use gcc -H <compiler-options> myfile.c. That will list the included files. If your myfile.h.gch does not appear, or has an x, then something is wrong. If it appears with a !, congratulations! You are using it.

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

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.

file not recognized: File format not recognized error in C

dico.h.gch is a precompiled header. You aren't supposed to link it with your objects. Remove it from the line that creates Tuto.exe.



Related Topics



Leave a reply



Submit