14

Back a long time ago I used to use pre-compiled headers: a. to speed compilation and b. because I supported multiple development tools like CodeWarrior, MPW, VS, ProjectBuilder, gcc, intel compilers, etc, etc.

Now I have a Mac Pro with 32gb of RAM.

Now I use just CMake.

So do we really need pre-compiled headers any more?

Are there obvious benefits that I just dont see/know?

How can one make a cross-platform pre-compiled header? Maybe that would simplify my life too.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 8
    Can you tell us what advantages you think the precompiled headers had, but have no longer? – avakar Jul 16 '09 at 15:14
  • @avakar - I am not saying they dont have the same advantages, but with systems getting faster daily is it worth the marginal performance gain for the headaches they involve? –  Jul 16 '09 at 15:23
  • @Miky D - WEll, maybe marginal is the wrong word, but what are concrete numbers? Say building a complex app like Photoshop. With and without using PCH. Maybe my projects have just always been small enough I just dont notice a huge difference –  Jul 16 '09 at 15:27
  • Halving the build time is not marginal - but it depends on the size of the project and the physical dependencies of the code. If you can't speed up your build much with PCHs, your code probably has very healthy physical structure. – Joris Timmermans Jul 16 '09 at 15:30
  • I'd disagree. Complex stuff like or a bunch of STL headers will be used in many places. Having a single PCH shared between 100+ source files will save time even on well-structured programs. – MSalters Jul 17 '09 at 10:57
  • Related: http://qualitycoding.org/precompiled-headers/ – Thomas Eding May 05 '14 at 18:38

7 Answers7

19

There is no such thing as a build that is "Fast enough". Proponents of TDD (Test-Driven Development) will be upset if their build takes longer than a few seconds because it makes turnaround on their development style much slower.

I've worked on projects with hours of compilation time that we halved (or better) by working with pre-compiled headers the right way, so the benefit can be quite large especially for projects that have neglected build times for a while.

However, the preferred solution remains that compilation times never get that far out of hand, by controlling the physical dependencies of the code.

For more information read The Care and Feeding of Pre-compiled Headers

Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75
5

I think that your question really depends on the size of the project you are compiling. There are some really large projects out there where precompiled headers do make a huge difference.

So, my answer is: it depends..

But if using them makes you life miserable for some reason or another (i.e. portability) and your project is not incredibly large.. I'd say skip them.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
3

The only obvious benefit is a slight decrease in compilation time. I generally don't use them. On most of my projects, the compilation times are not that bad (e.g. usually less than a few minutes).

Unless your project is enormous, they usually do more harm than good. (They are highly compiler dependent.)

Zifre
  • 26,504
  • 11
  • 85
  • 105
3

I decided to try that today. I use Microsoft Visual C++ 2010.

WIthout precompiled header, total compilation time 3.4s. With precompiled headers, total compilation time 1.5s. So for me, it is worth using it.

Precompiled header contained a lot of boost and stl headers and windows.h. Total size of generated precompiled header file (pch) file 49Mbytes.

Aftershock
  • 5,205
  • 4
  • 51
  • 64
2

I'm always turning it off. I've run into more problems than good with them on the projects I've worked on. Many subtle bugs came up that could be tracked down to a precompiled header that did not get recompiled for some reason. Maybe it's better in current VS incarnations.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74
0

The advantage of precompiled headers is that the huge system library header files and other files that do not change at all or infrequently only have to be parsed once per build.

As all C compilers (that I know of) work on every .c file seperately, skipping the "known" part of the includes can have a huge impact on compile time.

They are not a portability issue either, you can compile a project using stdafx.h/cpp with GCC just fine, the only problem is that the compilation time might go up because not everything in stdafx.h is needed in every compilation unit.

Timbo
  • 27,472
  • 11
  • 50
  • 75
  • MSVC works on multiple source files physically, but keeps them logically separate. – MSalters Jul 17 '09 at 10:59
  • Thats why MSVC is still benefitting much much more from precompiled headers then gcc and clang. We need to move away from compiler programs to compiler servers so a precompiled header is always loaded. – Lothar Feb 19 '17 at 12:13
-1

It's still useful to use precompile header for at least two reasons :
1) A lot of people don't have a Mac Pro with 32 GB RAM, lucky man !
2) It's a way to limit inclusion in *.h
The only way that I know to make cross-platform pre-compiled header is to work with Qt and its cross platform project file : the PRO file !

Matthieu
  • 2,743
  • 19
  • 21
  • 3
    pre-compiled headers end up using more ram... I don't understand your comment. – Marius Jan 21 '10 at 20:31
  • I mean a powerful machine, RAM is just a descriptive example... Obviously, RAM doesn't make compilation faster ! – Matthieu Feb 03 '10 at 10:22