Problem
Say you have a list of headers that you use that you know will not change. For example, C headers or C ++ headers or Boost headers, etc.
Reading them for each compilation of the CPP file takes time, and this is not a productive time, since the compiler reads the same headers over and over and creates the same compilation result for the same headers over and over again.
There must be some way to tell the compiler that these headers are always the same, and cache their compiled result, rather than recompiling them again and again, no?
Decision
Precompiled headers take this into account, so all you need is:
- Put all of these common and immutable included in one header file (say StdAfx.h)
- You have one empty CPP file (say StdAfx.cpp) including only this header file
And now you need to tell the compiler that StdAfx.cpp is an empty source that contains regular and immutable headers.
The flags / Yc and / Yu are used here:
- Compile the StdAfx.cpp file with the / Yc flag
- Compile all other CPP files with the / Yu flag
And the compiler will generate (if necessary) a precompiled header file from the StdAfx.cpp file, and then reuse this precompiled header file for all other files marked with / Yu.
Note
When you create a new project, older versions of Visual C ++ (6 and 2003, if I remember correctly) activated precompiled headers by default. Recent ones offer the option to activate them not.
You need to create a new VC ++ project with PCH activated in order to have a working version of the project with PCH support, and explore compilation options.
For more information about PCH, you can visit the following URL:
paercebal
source share