Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement precompiled headers into your project

Tags:

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

like image 438
Chris Avatar asked Oct 11 '09 23:10

Chris


People also ask

How do I set up precompiled headers?

The compiler options for precompiled headers are /Y . In the project property pages, the options are located under Configuration Properties > C/C++ > Precompiled Headers. You can choose to not use precompiled headers, and you can specify the header file name and the name and path of the output file.


2 Answers

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 
like image 84
ChrisW Avatar answered Oct 04 '22 19:10

ChrisW


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)
like image 20
Spencer Rose Avatar answered Oct 04 '22 18:10

Spencer Rose