0

First of all i want to say that I read about precompiled headers and I understand that this is an optimization that saves me the time of compiling headers over and over on every built.

I'm reading the documentation of boost and I see that in the instructions they say:

In Configuration Properties > C/C++ > Precompiled Headers, change Use Precompiled Header (/Yu) to Not Using Precompiled Headers

And then they explain it:

There's no problem using Boost with precompiled headers; these instructions merely avoid precompiled headers because it would require Visual Studio-specific changes to the source code used in the examples.

  1. Can some explain me the sentence I marked in bold? which visual studio specific changes they are talking about ? (Here is the link to the documentation I'm reading: http://www.boost.org/doc/libs/1_55_0/more/getting_started/windows.html#pch)
  2. Why and when I would want to turn off the precompiled headers?
  3. what is the difference between "Create" and "Use" in the precompiled header options.
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
OopsUser
  • 4,642
  • 7
  • 46
  • 71
  • 2
    Umm, you'd need to include the precompiled header. – chris Jan 19 '14 at 21:30
  • 2
    its their way of saying the samples don't follow the mantra of a unified use-this-lead-in-header-for-pch-generation model. IOW, their *samples* aren't PCH-friendly, but you can still use pch with boost in *your* projects if properly configured correctly. – WhozCraig Jan 19 '14 at 22:09
  • precompiled headers are really not an exclusive of Visual Studio, for example QtCreator supports `pch` files too http://qt-project.org/doc/qt-4.8/qmake-precompiledheaders.html – user2485710 Jan 19 '14 at 22:21
  • That's gcc or clang supporting them, not QtCreator :) – sehe Jan 19 '14 at 23:27
  • @sehe ... yes and no, you still need the support from your IDE to make everything work. Anyway my point was that a `pch` file it's not a VS only thing. – user2485710 Jan 20 '14 at 00:32
  • @user2485710 Wut? I've been using precompiled headers no worries without touching an IDE for years. But, you know. (GCC likes to call those gch and make has builtin rules to deal with them) – sehe Jan 20 '14 at 00:35
  • @user2485710: Total nonsense! – Lightness Races in Orbit Jan 20 '14 at 00:37
  • @LightnessRacesinOrbit no need for automatic file recognition ? autocomplete feature ? online help ? are you sure ? – user2485710 Jan 20 '14 at 00:42
  • 1
    @user2485710: "Need"? No, of course not. Do you _need_ your luxury car? That holiday you took last year? You said you "need" an IDE to "make everything work", not to make things easier for you. – Lightness Races in Orbit Jan 20 '14 at 00:47
  • 3
    Those things are not technically required to operate the pch feature of your compiler. – Puppy Jan 20 '14 at 00:48
  • @LightnessRacesinOrbit if you are using an IDE that means that you made your choices about having or not a luxury car ... I never said "required", I said supported by. – user2485710 Jan 20 '14 at 00:49
  • 1
    *you still need the support from your IDE* pretty much reads as *required*. – Bartek Banachewicz Jan 20 '14 at 00:52
  • @user2485710: http://www.thefreedictionary.com/need – Lightness Races in Orbit Jan 20 '14 at 00:53
  • @LightnessRacesinOrbit in that phrase the IDE was the subject and the precompiled header the feature, the verb `to need` was referred to the IDE, because it can't work well without knowing what a pch file is, is that hard to comprehend ? damn english, just invent a much simpler language ... – user2485710 Jan 20 '14 at 00:55
  • 1
    @user2485710: It's not complicated. You don't need an IDE to make precompiled headers work. Simple as that. – Lightness Races in Orbit Jan 20 '14 at 00:57
  • @LightnessRacesinOrbit I know that, I was trying to express the fact that if you open QtCreator and that program doesn't know anything about the feature X you are not helping yourself ... sometimes the problem is just to express yourself, not what you know or what you don't. – user2485710 Jan 20 '14 at 00:58

3 Answers3

4

Originally a comment, but I may as well post it. Note: this is specific to VC++:

  1. The bold sentence is their way of saying the samples don't follow the mantra of a unified use-this-lead-in-header-for-pch-generation model. IOW, their samples aren't PCH-friendly, but you can still use pch with boost in your projects if properly configured.

  2. You would turn them off for a variety of reasons. Some source modules, particularly ones from 3rd-parties, don't follow the PCH model of including "the" pch-through-header at their outset. Their samples are such code (and thus the advise to turn them off for their samples). Sometimes source files require different preprocessor configurations only for this files and not all files int he project; another reason to disable PCH for those files.

  3. You typically use a source/header pair to generate "the One"; the precompiled header image. This header file typically includes:

    1. Any system standard lib headers used by your project
    2. 3rd-party SDK headers
    3. Just about everything else that is NOT in active development for your project.

The single source file tagged as Create typically includes one line of code : #include "YourHeaderFile.h", where YourHeaderFile.h is the header you filled with stuff from the list above. Tagging it as "Create" through header YourHeaderFile.h tells VC it is the file needed for rebuilding the PCH through that header when compiling other source files. All other source files are tagged as Use (except the ones where PCH is turned off) and should include, as their first line of code, the same #include "TheHeaderFile.h".

In short (hard to believe), <boost> is telling you their samples aren't setup like described above, and as such you should turn PCH off when building them.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
3

When you use pre-compiled headers, you need to do something like:

#include <foo>
#include <bar>
#include <baz>

#pragma hdrstop

// other code here

Everything before the #pragma goes into the precompiled header. Everything after it depends on the precompiled header. The VC++ specific "magic" to make pre-compiled header work is that #pragma.

There's a little more to the story than just that though. To make pre-compiled headers work well, you want to include exactly the same set of headers in exactly the same order in every source file.

That leads to (typically) creating one header that includes all the other common headers and has the #pragma hdrstop right at its end, then including that in all the other source files.

Then, when the compiler does its thing, there are two phases: first you need to create a pre-compiled header. This means running the compiler with one switch. The compiler only looks at what comes before the #pragma hdrstop, builds a symbol table (and such) and puts the data into a .pch file.

Then comes the phase when you do a build using the pre-compiled header. In this phase, the compiler simply ignores everything in the the file up to the #pragma hdrstop. When it gets to that, it reads the compiler's internal state from the .pch file, and then starts compiling that individual file.

This means each source file typically includes a lot of headers it doesn't actually need. That, in turn, means that if you don't use pre-compiled headers, you end up with compilation that's much slower than if you hadn't done anything to support pre-compiled headers at all.

In other words, although the only part that's absolutely required is the #pragma hdrstop, which is fairly innocuous, a great deal more file re-structuring is needed to get much benefit from them--and those changes are likely to actively harmful to compilation time if you're using anything that doesn't support pre-compiled headers (and in the same way VC++ does them at that).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Except that in Visual C++, instead of `#pragma hdrstop`, it matches the filename used in one of the `#include` directives. – Ben Voigt Jan 20 '14 at 00:46
1

When precompiled headers is on every cpp source file must start with #include "stdafx.h"

So you would turn it off if you do not want to edit all the boost source files.

When precompiled headers is on stdafx.cpp "creates" the precompiled header. All other files "use" the precompiled header.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • 3
    It doesn't have to be called "stdafx.h" (that's an setting), and it doesn't have to be `#include`d, there is an advanced option to force including it. – kfsone Jan 19 '14 at 21:43