How to efficiently use precompiled headers (using / Yc and Yu)?

We use Visual Studio 2003 (VC71) to compile. To reduce compilation time, we redesigned the script so that it generates a file with a precompiled header (.pch) for each CPP file.

Option used in makefile:

/Yc"StdAfx.h" /Fp"StdAfx.pch" 

At the same time, compilation time for the target decreased by 30%. But can anyone help me understand how this reduces compiler time, even when the pch file is created each time to compile each CPP file.

Also, is this the right approach? Should we use a combination of Yc and Yu? I cannot use the / Yu option, since the pch file must be destroyed at least once.

+6
c ++ visual-studio build precompiled-headers
source share
2 answers

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:

+19
source share

/ Yc should only be used on one of your .cpp modules. This points to VS to create a precompiled header with this module.

For everyone else in your project, use / Yu. This indicates that they should just use pch.

The MSDN entry for it is here: http://msdn.microsoft.com/en-us/library/szfdksca(v=VS.71).aspx

+2
source share

All Articles