Click here to Skip to main content
11,408,708 members (65,094 online)
Click here to Skip to main content

Including stdafx.h in a non-default location

, 11 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
The vc compiler and intellisense don't look for pre-compiled header in a similar manner, this is a summary of the issue along with workarounds
The vc compiler automatically disambiguates the location of stdafx.h when you #include "stdafx.h", using a set of defined rules (see MSDN for specifics), BUT IntelliSense does not. Therefore even when a piece of code compiles successfully, IntelliSense will not work correctly because it doesn't use the same logic as the compiler. If you specify the exact location of the file, as in #include "..\\stdafx.h", IntelliSense will now work right, but the compiler will call an error since it's looking for #include "stdafx.h" and not any other variant (bad string compare logic).

What makes this worse? Microsoft claims this is by design ([^]).

There are a couple of ways to get around this "feature". Do a double include since the header guard should prevent problems:
#include "stdafx.h"      //Pre-compiled header for compiler
#include "..\\stdafx.h"  //Exact location of pre-compiled header for intellisense

Or include the location of stdafx.h as one of the project's default directories. A less preferable method in my case since I'm developing the code to eventually transfer to a larger project.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Albert Holguin
United States United States
I'm an angry monkey. Big Grin | :-D

Comments and Discussions

QuestionPossible fix Pinmemberrobindegen20-Aug-12 23:48 
GeneralRe: ...and it is true that you can specify the file name, in you... PinmemberAlbert Holguin19-Apr-11 13:15 
GeneralRe: sorry but i'm not about to download a zip file from the inte... PinmemberAlbert Holguin19-Apr-11 13:12 
GeneralRe: Unfortunately, successful compilation does not imply well-fo... PinmemberVyacheslav Lanovets19-Apr-11 12:27 
Generalno comment PinmemberRedDK22-Jul-11 8:00 
GeneralThe problem is that this is not a valid C++ program. There i... PinmemberXentrax19-Apr-11 4:35 
GeneralRe: Since this was moved and posted as a comment, I'll answer ag... PinmemberAlbert Holguin19-Apr-11 4:43 
Generalincorrect pch file handling Pinmemberjustinh19-Apr-11 9:40 
GeneralRe: incorrect pch file handling PinmemberAlbert Holguin19-Apr-11 10:58 
GeneralAlternatives PinmentorHans Dietrich11-Apr-11 17:07 
GeneralRe: Alternatives PinmemberAlbert Holguin11-Apr-11 17:12 
AnswerRe: Alternatives PinmentorHans Dietrich12-Apr-11 0:03 
GeneralRe: Alternatives PinmemberAlbert Holguin12-Apr-11 5:00 
GeneralRe: Alternatives PinmemberAlbert Holguin12-Apr-11 5:06 
GeneralRe: Alternatives PinmemberAnna-Jayne Metcalfe18-Apr-11 22:44 
GeneralRe: Alternatives PinmemberAlbert Holguin19-Apr-11 4:47 
GeneralRe: Alternatives PinmemberAnna-Jayne Metcalfe19-Apr-11 5:22 
QuestionRe: Alternatives PinmemberAlbert Holguin19-Apr-11 5:32 
AnswerRe: Alternatives PinmemberAnna-Jayne Metcalfe19-Apr-11 5:44 
GeneralRe: Alternatives PinmemberAlbert Holguin19-Apr-11 6:02 
GeneralRe: Alternatives PinmemberAnna-Jayne Metcalfe19-Apr-11 11:53 
GeneralRe: Alternatives PinmemberAlbert Holguin19-Apr-11 12:20 
GeneralRe: Alternatives PinmemberAnna-Jayne Metcalfe19-Apr-11 22:42 
GeneralRe: Alternatives PinmemberAlbert Holguin20-Apr-11 3:08 
GeneralRe: Alternatives PinmemberAnna-Jayne Metcalfe20-Apr-11 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 11 Apr 2011
Article Copyright 2011 by Albert Holguin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid