How to use precompiled headers efficiently (using /Yc and Yu options)?
Asked Answered
D

2

10

We are using Visual Studio 2003 (VC71) for compilation. In order to reduce the compile time we changed the build script such that it generates the precompiled header (.pch) file for each CPP file.

The option used in makefile:

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

With this the compile time for the target got reduced by 30%. But could anyone help me to understand how is it reducing the compiler time even when the pch file is getting generated every time for compilation of each CPP file.

Also, is it the right approach? Should we use Yc and Yu combination ? I cannot use /Yu option as the pch file should be genrated at least once.

Deicer answered 3/3, 2011 at 5:33 Comment(2)
VS 2003? That's aaaaaaaaages agooooooo...Arcanum
We have plans to migrate to 2008\2010 but as of now its a legacy system using VC71Deicer
S
23

The problem

Let's say you have a list of headers you use that you know won't change. For example, the C headers, or the C++ headers, or Boost headers, etc..

Reading them for each CPP file compilation takes time, and this is not productive time as the compiler is reading the same headers, again and again, and producing the same compilation result for those same headers, again and again.

There should be some way to tell the compiler those headers are always the same, and cache their compiled result instead of recompiling them again and again, no?

The solution

The Pre-Compiled Headers takes that into account, so all you need is to:

  1. Put all those common and unchanging includes in one header file (say, StdAfx.h)
  2. Have one empty CPP file (say, StdAfx.cpp) including only this one header file

And now, what you need is tell the compiler that StdAfx.cpp is the empty source that includes the common and unchanging headers.

This is where the flags /Yc and /Yu are used:

  • Compile the StdAfx.cpp file with the /Yc flag
  • Compile all the others CPP files with the /Yu flag

And the compiler will generate (when needed) a pre-compiled header file from the StdAfx.cpp file, and then reuse this pre-compiled header file for all other files marked with /Yu.

Note

When you create a new project, old versions of Visual C++ (6 and 2003, if I remember correctly) would activate the precompiled headers by default. Recent ones offer the choice of activating them of not.

You should create a new VC++ project with the PCH activated to have a working version of PCH-enabled project, and study the compilation options.

For more information about PCH, you can visit the following URL:

Sacrosanct answered 3/3, 2011 at 8:53 Comment(3)
you say And the compiler will generate (when needed) a pre-compiled header file from the StdAfx.cpp file, and then reuse this pre-compiled header file for all other files marked with /Yu. what is when needed? does it checks the modification date of the ".h" files? If I have an ".h" file which doesn't change a lot, if I put it in the StdAfx.h will he catch the changes?Cordwood
@Roee Gavirel: If I have an ".h" file which doesn't change a lot, if I put it in the StdAfx.h will he catch the changes? : This is the idea, yes. I don't know how the compiler knows the file changed (I guess the modification date, as you suggested), but when using the StdAfix.h file, you should put inside it only files that rarely change.Sacrosanct
Is it possible to use /Yc in project A and then use those PCH'es in project B by using /Yu?Sic
S
2

/Yc should only be used on one of your .cpp modules. This specifies to VS to create the precompiled header with that module.

For all others in your project, use /Yu. This specifies that they are to simply use the pch.

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

Satanism answered 3/3, 2011 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.