Click here to Skip to main content
11,931,813 members (65,619 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked

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

You may also be interested in...

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.151126.1 | Last Updated 11 Apr 2011
Article Copyright 2011 by Albert Holguin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid