views:

925

answers:

2

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

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.

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?

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?

+1  A: 

This sounds like a case of "diminishing returns" to me. Suppose including the common headers directly wastes 1 second per .cpp file, and each target (DLL/EXE) has 10 .cpp files. By using a .pch per target, you save 10 seconds per target. If your whole project has 10 targets, you save 1.5 minutes on the whole build, which is good.

But by reducing it to one .pch for the whole project, you'd only save a further 9 seconds. Is it worth it? The extra effort (which may be a lot more fiddly to set up, being a non-standard configuration unsupported by VS wizards) produces only a 10th of the saving.

Daniel Earwicker
Think 200 projects in the solution with a pch that can take 10 seconds to compile, and then it's not so clear if the returns are so diminished.
Assaf Lavie
How long does the whole build take?
Daniel Earwicker
It also a matter of saving time on incremental builds. A change might force only one file in each project to recompile, then it takes a long time to load each pch to do a fast compile.
Marcus Lindblom
+2  A: 

It seems it's not possible because each source file has to be compiled against the same PDB against which the PCH was compiled. darn it.

Assaf Lavie
Yeah, I hit this too. :-(Error C2859: vc90.pdb is not the pdb file that was used when this precompiled header was created, recreate the precompiled header.http://msdn.microsoft.com/en-us/library/3bw58yy6.aspx
Trevor Robinson