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.