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.
By precompiling these headers, you can reduce the time a program takes to compile. Although you can use only one precompiled header ( . pch ) file per source file, you can use multiple . pch files in a project.
Configure Visual Studio to create precompiled headerschoose "All Configurations", then go to C/C++ -> Precompiled Headers, choose "Create Precompiled Header", make sure stdafx. h is the header file to use, and leave the rest to the default value.
Usage of precompiled headers may significantly reduce compilation time, especially when applied to large header files, header files that include many other header files, or header files that are included in many translation units.
To turn off precompiled headersSelect the Configuration properties > C/C++ > Precompiled Headers property page. In the property list, select the drop-down for the Precompiled Header property, and then choose Not Using Precompiled Headers. Choose OK to save your changes.
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 Pre-Compiled Headers takes that into account, so all you need is to:
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:
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With