24

I understand the purpose and reasoning behind precompiled headers. However, what are the rules when implementing them? From my understanding, it goes something like this:

  1. Set your project up to use precompiled headers with the YU directive.
  2. Create your stdafx.h file and set that to be your precompiled header.
  3. Include this as the top include statement in each of your .h files.

It this correct? Should you exclude the including it in the files that are included within your precompiled header? Currently, I get the following compilation error when following my intuition with this:

error C2857: '#include' statement specified with the /Ycstdafx.h command-line option was not found in the source file

The command-line options are as such:

/Od /I "../External/PlatformSDK/Include" /I ".." /I "../External/atlmfc/Include" /D "_DEBUG" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Yc"stdafx.h" /Fp"....\Output\LudoCore\Debug\LudoCore.pch" /Fo"....\Output\LudoCore\Debug\" /Fd"....\Output\LudoCore\Debug\vc80.pdb" /W4 /WX /nologo /c /ZI /TP /wd4201 /errorReport:prompt

Chris
  • 21,549
  • 25
  • 71
  • 99

4 Answers4

38

You stdafx.cpp should include stdafx.h and be built using /Yc"stdafx.h".

Your other *.cpp should be include stdafx.h and be built using /Yu"stdafx.h".

Note the double-quote characters used in the compiler options!

Here's a screenshot of the Visual Studio settings for stdafx.cpp to create a precompiled header:

create precompiled header

Here are the corresponding command-line options (which are read-only but reflect the settings specified on other pages; note that the IDE inserts double-quote characters around the filename, in the compiler option):

options

This is what's in my stdafx.cpp file:

// stdafx.cpp : source file that includes just the standard includes
// CallWinsock.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
ChrisW
  • 54,973
  • 13
  • 116
  • 224
  • Are you saying that this should only be built at command line? In visual studio, I'm not sure how to create something with a certain property right off the bat. It always seems to be just "Add -> New Item -> Header", etc – Chris Oct 12 '09 at 00:42
  • Right-click on the cpp file and select "Properties" on the context menu. Among the C++ configuration property pages, there's one for "Precompiled Headers", which you use to specify that this CPP file either creates or uses a precompiled header file. – ChrisW Oct 12 '09 at 00:50
  • Thanks for the update. I have done as shown above and still no dice. Here is the command line options with respect to yours above: /Od /I "../External/PlatformSDK/Include" /I ".." /I "../External/atlmfc/Include" /D "_DEBUG" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Yc"stdafx.h" /Fp"..\..\Output\LudoCore\Debug\LudoCore.pch" /Fo"..\..\Output\LudoCore\Debug\\" /Fd"..\..\Output\LudoCore\Debug\vc80.pdb" /W4 /WX /nologo /c /ZI /TP /wd4201 /errorReport:prompt – Chris Oct 12 '09 at 01:49
  • What are the contents of your stdafx.cpp (show it by editing and adding it to your original question)? In which directory is your stdafx.h? – ChrisW Oct 12 '09 at 01:56
  • If it had contents I would add it to the question, but it is empty, except for including the respective header file right now. – Chris Oct 12 '09 at 02:00
  • What exactly does your include statement look like? The error message you cited says that it can't find the include statement in the source file. – ChrisW Oct 12 '09 at 02:04
  • #include "stdafx.h" this is the only line of code in stdafx.cpp – Chris Oct 12 '09 at 02:17
  • Note that these two files did not generate for me automatically. I manually added them. Is this a part of the problem? (just noticed this might clear some things up) – Chris Oct 12 '09 at 02:17
  • Does your stdafx.h exist in the same directory as your stdafx.cpp? – ChrisW Oct 12 '09 at 02:22
  • Yes. Also, they are the only two files at the "root" of the project. All of the rest of the files in this project are grouped into folders. – Chris Oct 12 '09 at 02:23
  • I don't see what's wrong; you ought to be able to create them (manually) later, and not only when you first create the project. I get the same error message as you, if I comment-out the include statement in my stdafx.cpp, or if I alter the include statement to include a different (but not non-existent) header file. – ChrisW Oct 12 '09 at 02:35
  • @ChrisW - Sorry for the nerco but your images are down. Any chance you could reupload them? – Gareth Jones Jul 10 '14 at 03:31
  • @GarethJones Thank you, I re-uploaded the images. – ChrisW Jul 10 '14 at 07:04
10

Visual studio can store pre-compiled header properties not just by project but by source file.

By default all properties are set to "inherit from parent or project defaults" which just shows the parent object's value for that property, but once overriden at a child level (in this case a .cpp file) the file ignores changes from the parent property.

The configuration you want is:

  1. At project level with "All Configurations" selected (project properties, change configuration drop down from debug/release), Set your pre-compiled header to "Use"
  2. Pick a .cpp file to be the source of the header (as far as I know doesn't matter which one)
  3. Right click and goto properties, and select "Create" for precompiled header
  4. Finally make sure that no other .cpp files have values set for the precompiled header property (you can tell by the bold text)
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
Spencer Rose
  • 1,190
  • 12
  • 21
5

Your #include "stdafx.h" should be the first line of each cpp file. It shouldn't be used in the .h files. Other than that you're about right.

Alan
  • 4,897
  • 2
  • 24
  • 17
5

"stdafx" is just a convention. It's in no way mandatory. In a multi-project solution, I've used other setups with multiple pre-compiled headers for different parts. E.g. it may be useful to have one PCH shared by your UI projects, and another one for your database projects.

The relevant components are the X.h file listing precompilable headers, the X.cpp file that includes only X.h (and adds no code itself), and the X.pch file created by compiling X.cpp (and thus X.h) with compiler option /Yc.

When you're now compiling Y.cpp file with /Yu"X.pch", the compiler reads and discards everything up to #include "X.h". At that point it replaces its internal state with the state stored in X.pch, except for the input stream (remains Y.cpp, with the file pointer set to the next line after #include "X.h").

MSalters
  • 173,980
  • 10
  • 155
  • 350