简体   繁体   English

如何有效地使用预编译头文件(使用/ Yc和Yu选项)?

[英]How to use precompiled headers efficiently (using /Yc and Yu options)?

We are using Visual Studio 2003 (VC71) for compilation. 我们使用Visual Studio 2003(VC71)进行编译。 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. 为了减少编译时间,我们更改了构建脚本,以便为每个CPP文件生成预编译头文件(.pch)。

The option used in makefile: makefile中使用的选项:

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

With this the compile time for the target got reduced by 30%. 这样,目标的编译时间减少了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. 但是,任何人都可以帮助我理解即使每次编译每个CPP文件时生成pch文件,它如何减少编译时间。

Also, is it the right approach? 还有,这是正确的方法吗? Should we use Yc and Yu combination ? 我们应该使用Yc和Yu组合吗? I cannot use /Yu option as the pch file should be genrated at least once. 我不能使用/ Yu选项,因为pch文件应该至少生成一次。

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.. 例如,C头,或C ++头,或Boost头等。

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. 为每个CPP文件编译读取它们需要花费时间,这不是生产时间,因为编译器一次又一次地读取相同的标题,并且一次又一次地为这些相同的标题生成相同的编译结果。

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) 将所有常见且不变的包含放在一个头文件中(例如,StdAfx.h)
  2. Have one empty CPP file (say, StdAfx.cpp) including only this one header file 有一个空的CPP文件(比如说StdAfx.cpp),只包含这一个头文件

And now, what you need is tell the compiler that StdAfx.cpp is the empty source that includes the common and unchanging headers. 现在,您需要告诉编译器StdAfx.cpp是包含公共和不变标头的空源。

This is where the flags /Yc and /Yu are used: 这是使用flags / Yc和/ Yu的地方:

  • Compile the StdAfx.cpp file with the /Yc flag 使用/ Yc标志编译StdAfx.cpp文件
  • Compile all the others CPP files with the /Yu flag 使用/ Yu标志编译所有其他CPP文件

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. 编译器将从StdAfx.cpp文件生成(在需要时)预编译的头文件,然后将这个预编译的头文件重用于标记为/ 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. 当您创建一个新项目时,旧版本的Visual C ++(6和2003,如果我没记错的话)将默认激活预编译的头文件。 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. 您应该创建一个新的VC ++项目,并激活PCH以获得启用PCH的项目的工作版本,并研究编译选项。

For more information about PCH, you can visit the following URL: 有关PCH的更多信息,您可以访问以下URL:

/Yc should only be used on one of your .cpp modules. / Yc只应在您的某个.cpp模块上使用。 This specifies to VS to create the precompiled header with that module. 这指定VS使用该模块创建预编译头。

For all others in your project, use /Yu. 对于项目中的所有其他人,请使用/ Yu。 This specifies that they are to simply use the pch. 这指定他们只是简单地使用pch。

The MSDN entry for it is here: http://msdn.microsoft.com/en-us/library/szfdksca(v=VS.71).aspx 它的MSDN条目在这里: http//msdn.microsoft.com/en-us/library/szfdksca(v = VSS71).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM