简体   繁体   English

有效地共享预编译的标题

[英]Sharing Pre-compiled Headers efficiently

I have a framework which is being used by several projects (which includes several samples to show how the framework works). 我有一个框架,正在被几个项目使用(其中包括几个示例以显示框架是如何工作的)。 The framework has components such as the core, graphics, physics, gui etc. Each one is a separate library. 该框架具有核心,图形,物理,gui等组件。每个组件都是一个单独的库。 There are several configurations as well. 有几种配置。

A main solution file compiles the complete project with all the possible configurations so that the projects can use the libraries. 主解决方案文件使用所有可能的配置编译整个项目,以便项目可以使用库。 Since the framework is rarely recompiled, especially by someone (including me) working on a project that utilizes the framework, it makes sense to pre-compile the many headers. 由于框架很少被重新编译,特别是由某人(包括我)处理使用该框架的项目,因此预编译许多标头是有意义的。

Initially I had each project/sample have its own pre-compiled header used for the whole project. 最初我让每个项目/样本都有自己的预编译头,用于整个项目。 Each time I would have to rebuild the same pch (for example, Debug), So I decided that a shared PCH would reduce the redundant PCH compilation. 每次我必须重建相同的pch(例如,Debug),所以我决定共享PCH将减少冗余PCH编译。 So far so good. 到现在为止还挺好。 I have a project that compiles the PCH along with the libraries. 我有一个项目,编译PCH和库。 All the subsequent projects/samples are now using the same PCH. 所有后续项目/样本现在都使用相同的PCH。 This has worked wonderfully. 这非常有效。

The only problem is that I have seen an increase in file size. 唯一的问题是我看到文件大小增加了。 This is not a roadblock, as if a project that uses the framework is intended to be released, it can sever itself from the shared PCH and make its own. 这不是障碍,就好像要发布使用该框架的项目一样,它可以将自己从共享的PCH中切断并自行创建。 I have done this for the sake of rapid development (I have actually created a tool which creates the VS project files and source files for a new project/sample ready to be built as well as facilitate in upgrading a previous project that was using an older version of the framework). 为了快速开发,我已经这样做了(我实际上已经创建了一个工具,可以为新项目/样本创建VS项目文件和源文件,以便于构建以及升级以前使用旧版本的项目框架的版本)。

Anyway, (I am presuming that) the increase in file size is because the independant VS project file that is creating the shared PCH is including all the headers from all the libraries. 无论如何,(我假设)文件大小的增加是因为创建共享PCH的独立VS项目文件包括来自所有库的所有头文件。 My question is whether I can use conditional compilation (#ifndef) to reduce the size of the final executable? 我的问题是我是否可以使用条件编译(#ifndef)来减少最终可执行文件的大小? Or maybe share multiple PCH files somehow (as far I know though, that is not possible, but I maybe wrong) If I am not making sense, please say so (in kind words :) ) as my knowledge of PCH files is very limited. 或者也许以某种方式共享多个PCH文件(据我所知,这是不可能的,但我可能错了)如果我没有意义,请说出来(实际上是:))因为我对PCH文件的了解非常有限。

Thanks! 谢谢!

Note: To re-iterate and make it clear, so far, I have one solution file that is compiling all the libraries including the shared PCH. 注意:为了重新迭代并清楚地说明,到目前为止,我有一个解决方案文件正在编译包括共享PCH在内的所有库。 Now if I recompile all the samples and projects, they compile in a couple of seconds or more at most. 现在,如果我重新编译所有示例和项目,它们最多可以在几秒或更长时间内编译。 Before, each project would recreate a PCH file. 之前,每个项目都会重新创建一个PCH文件。 Also, initially I wanted a PCH for each library, but then I found out that a source file cannot use multiple PCH files, so this option was not feasible. 另外,最初我想为每个库提供PCH,但后来我发现源文件不能使用多个PCH文件,所以这个选项不可行。 Another option is to compile all possible combinations of PCH files, but that is too time consuming and cumbersome and error prone. 另一个选择是编译PCH文件的所有可能组合,但这太耗时,麻烦且容易出错。

It sounds like the size problem is coming from using headers you don't actually need, but that it still makes sense to use these headers when developing because of the faster turn around. 听起来大小问题来自使用你实际上并不需要的标题,但是由于转换速度更快,在开发时使用这些标题仍然有意义。

On using #ifndefs: Precompilation is crude. 使用#ifndefs时:预编译很粗糙。 You lose the ability to share the precompilation work at the point where there is a difference. 您无法在存在差异的位置共享预编译工作。 If using #ifndefs to make different variants of what you include, Ie if you have 如果使用#ifndefs来制作你所包含内容的不同变体,即使你有

#ifndef FOO

Then the precompiled header must stop before the point where FOO is defined differently in two files that use that precompiled header. 然后,预编译头必须在使用该预编译头的两个文件中以不同方式定义FOO的点之前停止。 So #ifndef is not going to solve the problem. 所以#ifndef不会解决问题。 The net result is that FOO must be the same, or you're back to separate pch files for the different projects. 最终结果是FOO必须相同,或者您要回到不同项目的单独pch文件。 Neither solves things. 都不能解决问题。

As to sharing multiple .pch files: A fundamental limitation of .pch files is that each .obj can only use one. 至于共享多个.pch文件:.pch文件的一个基本限制是每个.obj只能使用一个。 Of course .pch files can have arbitrary combinations of headers. 当然.pch文件可以有任意组合的标题。 You could have a .pch for core+graphics, a .pch for core+physics, core+ai etc. This would work just dandy if none of the source files needed to 'talk' to more than core+one-module at a time. 你可以拥有一个核心+图形的.pch,一个核心+物理的核心,核心+ ai等。如果没有一个源文件需要与一个核心+一个模块进行“交谈”,这样就可以了。时间。 That does not sound realistic to me. 这对我来说听起来不太现实。 Such a scheme and variants on it sound like a lot of restructuring work for no real gain. 这样的计划和变体听起来像很多重组工作,没有真正的收获。 You don't want to be building zillions of combinations and keeping track of them all. 您不希望构建数以万计的组合并跟踪它们。 It's possible, but it is not going to save you time. 这是可能的,但它不会节省你的时间。

In my view you're doing exactly the right thing by sacrificing executable size for fast turn-around during development/debugging, and then having a slower but leaner way of building for the actual release. 在我看来,你通过牺牲可执行文件大小来实现正确的事情,以便在开发/调试期间快速周转,然后以更慢但更精简的方式构建实际版本。

In the past I've found that you quite quickly run into diminishing returns as you put more in the precompiled headers, so if you're trying to put more in to make it more useful in a larger number of projects then it will hit a point that it slows down. 在过去,我发现当你在预编译的头文件中放入更多内容时,很快就会遇到收益递减的问题,所以如果你想在更多的项目中投入更多资源,那么它将会更多指出它放慢了速度。 On our projects the PCH files take longer than most source files to compile, but still only a few seconds maximum. 在我们的项目中,PCH文件比大多数源文件需要更长的时间来编译,但仍然只有几秒钟。 I would suggest making the PCH files specific to each project you are using. 我建议制作特定于您正在使用的每个项目的PCH文件。 You are right in saying that a source file can only refer to a single PCH file, but one way of getting around this is to use the 'force include' option (in the Advanced tab I think) to ensure that all files include the PCH file for that project. 你是对的说源文件只能引用一个PCH文件,但解决这个问题的一种方法是使用'force include'选项(我认为在Advanced选项卡中)以确保所有文件都包含PCH该项目的文件。

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

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