What to put in precompiled header? (MSVC)
Asked Answered
L

4

40

What are the best candidates for a precompiled header file? Can I put STL and Boost headers there, even though they have templates? And will that reduce compile times? Also, what are the best IDE settings to reduce compile times?

Lavish answered 27/3, 2009 at 0:22 Comment(1)
I prefer not to use PCH. The reasons are (1) I like the first include in a cpp file to be its corresponding header so you know the header compiles (2) my experience is that dependency checing with pch is not fully reliable, and this results in incremental builds becoming unreliable in very large projects. In my opinion it is more efficient on balance not use pch and enjoy the benefits of reliable incremental builds. This might no longer be the case with the most recent Visual Studio versions - my experience with that is still not sufficient to judge.Viceroy
E
51

The quick answer: the STL and Boost headers do indeed belong in the precompiled header file, even though these header files define template classes.

When generating a precompiled header file, a compiler parses the header text (a significant task!), and converts it into a binary format that is optimised for the compiler's benefit.

Even though the template classes will be instantiated when other .cpp files are compiled, they will be instantiated from information in the precompiled header, which is significantly faster for the compiler to read.


(later addition)

One thing that you should not include in a precompiled header are files that are part of your project and are changed frequently, even if every single .CPP file includes these files.

The reason is this - the generation of the precompiled header can take a long time, because the boost, stl and windows libraries are very large.

You might have a simple file (eg "StringDefs.h") that everything uses. If StringDefs.h is included in stdafx.h, and one developer touches StringDefs.h, then every developer has to wait until the entire precompiled header recompiles. It would be much faster if StringDefs.h was left out of the precompiled header, and parsed along with each .CPP file.

Eudocia answered 27/3, 2009 at 0:40 Comment(5)
That's a fair point - including frequently changing files would slow down build time, overall.Sememe
You can only have 1 precompiled header file per .cpp file, but you can have multiple pre-compiled headers in your project.Mcginty
You not only have to wait for the PCH to recompile, but also for every file that uses it.Zwolle
Do you have any references to substantiate the claim that templates will indeed be improved? Eg profiling results, or compiler documentation links?Drumm
one developer touches StringDefs.h, then every developer has to wait until the entire precompiled header recompiles. I think this is the expected behavior if StringDefs.h is barely touched.Pitch
M
6

One addition to Andrew Shepherd's answer. Use the precompiled header for header files that are external to your project, for files that change infrequently. If you're changing the header files in the current project all the time, it's probably not worth precompiling them.

Marx answered 27/3, 2009 at 0:56 Comment(0)
I
4

I've written an article on techniques that reduce the compilation time. Among these techniques a post on precompiled header and its application can be found here. It also has a section on best practices that you may find interesting. CMake scripts that handle it transparently are included.

Ingate answered 21/2, 2010 at 18:58 Comment(1)
This is a nice article.Elongate
S
-1

Put anything in the precompiled header that most of the .cpp files in that project would include anyway. This goes for any header file, really. This allows the compiler to parse these files once, and then reuse that information in all .cpp files in the same project.

Sememe answered 27/3, 2009 at 0:47 Comment(4)
I disagree, and have written an extra section in my reply to explain why.Eudocia
precompiled headers should never include project-local headersOracular
I have worked extensively on a project that followed this erroneous advice and it is very inefficient - every time you compile, everything compiles for any change.Viceroy
"precompiled headers should never include project-local headers" Why not? If the local headers are stable and never change, and are included all through your application, why wouldn't you put them in the precompiled header?Broyles

© 2022 - 2024 — McMap. All rights reserved.