Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .h.gch files

I recently found out about precompiled headers in C++, specifically for gcc compilers. I did find something about the .h.gch files on the net, but haven't yet been able to use them.

I need to know:

  1. How to create a .h.gch file? My code isn't creating one?
  2. Once created, how to use it? do you include it like any other user-defined file? ex: #include "SomeCode.h.gch"

Or is there some other way of using it? please help

like image 417
zhirzh Avatar asked May 30 '14 20:05

zhirzh


People also ask

How do I run a header file in Ubuntu?

usually you type name-of-your-editor /usr/local/include/name-of your. h . For example vim /usr/local/include/foobar. h .

What is precompiled header C++?

In computer programming, a precompiled header (PCH) is a (C or C++) header file that is compiled into an intermediate form that is faster to process for the compiler.


1 Answers

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.

like image 165
rodrigo Avatar answered Oct 10 '22 02:10

rodrigo