在Visual Studio中的项目之间共享预编译头 [英] Sharing precompiled headers between projects in Visual Studio

查看:233
本文介绍了在Visual Studio中的项目之间共享预编译头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解决方案与许多Visual C ++项目,所有使用PCH,但一些有特定的编译器开关打开为项目的特定需求。

I have a solution with many Visual C++ projects, all using PCH, but some have particular compiler switches turned on for project-specific needs.

大多数项目在它们各自的stdafx.h(STL,boost等)中共享同一组头。我想知道是否可以在项目之间共享PCH,所以,而不是编译每个项目的PCH,我可能有一个常见的PCH,解决方案中的大多数项目可以使用。

Most of these projects share the same set of headers in their respective stdafx.h (STL, boost, etc). I'm wondering if it's possible to share PCH between projects, so that instead of compiling every PCH per-project I could maybe have one common PCH that most projects in the solution could just use.

似乎可以在项目设置中指定PCH的位置作为共享位置,所以我有一个hunch这可以工作。我还假设所有使用共享PCH的项目中的所有源文件必须具有相同的编译器设置,否则编译器会抱怨PCH和正在编译的源文件之间的不一致。

It seems possible to specify the location of the PCH as a shared location in the project settings, so I have a hunch this could work. I'm also assuming that all source files in all projects that use a shared PCH would have to have the same compiler settings, or else the compiler would complain about inconsistencies between the PCH and the source file being compiled.

有没有人试过这个?它是否工作?

Has anyone tried this? Does it work?

一个相关的问题:这样的分片PCH是否过分包容,还是会损害整体构建时间?例如,共享PCH可以包括广泛使用的许多STL头部,但是一些项目可能仅需要< string> < vector> 。当使用共享PCH节省的时间必须在构建过程的稍后时间被回收,当优化器必须丢弃PCH拖入项目中的所有未使用的东西时?

A related question: should such a shard PCH be overly inclusive, or would that hurt overall build time? For example, a shared PCH could include many STL headers that are widely used, but some projecst might only need <string> and <vector>. Would the time saved by using a shared PCH have to be paid back at a later point in the build process when the optimizer would have to discard all the unused stuff dragged into the project by the PCH?

推荐答案

是的,这是可能的,我可以向你保证,节省的时间是显着的。当你编译你的PCH,你必须从创建的项目中复制 .pdb .idb PCH文件。在我的情况下,我有一个简单的两个文件项目,创建一个PCH文件。标题将是您的PCH标题,并且源将被告知在项目设置下创建PCH - 这是类似于您在任何项目中通常会做的。正如你所提到的,你必须对每个配置有相同的编译设置,否则会出现差异,编译器会抱怨。

Yes it is possible and I can assure you, the time savings are significant. When you compile your PCH, you have to copy the .pdb and .idb files from the project that is creating the PCH file. In my case, I have a simple two file project that is creating a PCH file. The header will be your PCH header and the source will be told to create the PCH under project settings - this is similar to what you would do normally in any project. As you mentioned, you have to have the same compile settings for each configuration otherwise a discrepancy will arise and the compiler will complain.

每次有复制上述文件重建或每次PCH被重新编译将是一个痛苦,所以我们将自动化。要自动复制,请执行预构建事件,其中将上述文件复制到相应的目录。例如,如果您正在编译PCH的 Debug Release 构建,请从 Debug 到您的从属项目的 Debug 。所以复制命令看起来像这样

Copying the above mentioned files every time there is a rebuild or every time the PCH is recompiled is going to be a pain, so we will automate it. To automate copying, perform a pre-build event where the above mentioned files are copied over to the appropriate directory. For example, if you are compiling Debug and Release builds of your PCH, copy the files from Debug of your PCH project over to your dependent project's Debug. So a copy command would look like this


copy PchPath\Debug * .pdb Debug \ / -Y

copy PchPath\Debug*.pdb Debug\ /-Y

注意结束处的 / - Y 。第一次构建之后,每个后续构建都会逐步编译,因此如果再次替换这些文件,Visual Studio将会报告有关已损坏的符号。如果它们被损坏,你总是可以执行重建,这将再次复制文件(这一次它不会跳过它们,因为他们不再存在 - 清理删除文件)。

Note the /-Y at the end. After the first build, each subsequent build is incrementally compiled, therefore if you replace the files again, Visual Studio will complain about corrupted symbols. If they do get corrupted, you can always perform a rebuild, which will copy the files again (this time it won't skip them as they no longer exist - the cleanup deletes the files).

我希望这有助于。它花了我很多时间,能够做到这一点,但它是值得的。我有几个项目依赖于一个大框架,PCH只需要编译一次。所有的依赖项目现在编译得很快。

I hope this helps. It took me quite some time to be able to do this, but it was worth it. I have several projects that depend on one big framework, and the PCH needs to be compiled only once. All the dependent projects now compile very quickly.


编辑:与其他几个人一起,我测试了VS2010
, VS2012,它似乎工作正常。

Along with several other people, I have tested this under VS2010 and VS2012 and it does appear to work properly.

这篇关于在Visual Studio中的项目之间共享预编译头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆