Click here to Skip to main content
15,878,871 members
Articles / Desktop Programming / MFC
Tip/Trick

Including stdafx.h in a non-default location

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
11 Apr 2011CPOL 43.9K   3   25
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 (http://connect.microsoft.com/VisualStudio/feedback/details/533605/stdafx-h-cant-be-parsed-with-intellisense-squiggles-mechanism[^]).

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.

License

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


Written By
Engineer
United States United States
I'm an angry monkey. Big Grin | :-D

Comments and Discussions

 
QuestionPossible fix Pin
robindegen20-Aug-12 22:48
robindegen20-Aug-12 22:48 
GeneralRe: ...and it is true that you can specify the file name, in you... Pin
Albert Holguin19-Apr-11 12:15
professionalAlbert Holguin19-Apr-11 12:15 
GeneralRe: sorry but i'm not about to download a zip file from the inte... Pin
Albert Holguin19-Apr-11 12:12
professionalAlbert Holguin19-Apr-11 12:12 
GeneralRe: Unfortunately, successful compilation does not imply well-fo... Pin
Vyacheslav Lanovets19-Apr-11 11:27
Vyacheslav Lanovets19-Apr-11 11:27 
Generalno comment Pin
RedDk22-Jul-11 7:00
RedDk22-Jul-11 7:00 
GeneralThe problem is that this is not a valid C++ program. There i... Pin
Vyacheslav Lanovets19-Apr-11 3:35
Vyacheslav Lanovets19-Apr-11 3:35 
GeneralRe: Since this was moved and posted as a comment, I'll answer ag... Pin
Albert Holguin19-Apr-11 3:43
professionalAlbert Holguin19-Apr-11 3:43 
Generalincorrect pch file handling Pin
justinh19-Apr-11 8:40
justinh19-Apr-11 8:40 
GeneralRe: incorrect pch file handling Pin
Albert Holguin19-Apr-11 9:58
professionalAlbert Holguin19-Apr-11 9:58 
GeneralAlternatives Pin
Hans Dietrich11-Apr-11 16:07
mentorHans Dietrich11-Apr-11 16:07 
GeneralRe: Alternatives Pin
Albert Holguin11-Apr-11 16:12
professionalAlbert Holguin11-Apr-11 16:12 
AnswerRe: Alternatives Pin
Hans Dietrich11-Apr-11 23:03
mentorHans Dietrich11-Apr-11 23:03 
GeneralRe: Alternatives Pin
Albert Holguin12-Apr-11 4:00
professionalAlbert Holguin12-Apr-11 4:00 
GeneralRe: Alternatives Pin
Albert Holguin12-Apr-11 4:06
professionalAlbert Holguin12-Apr-11 4:06 
or maybe i didn't make that clear enough... ???
GeneralRe: Alternatives Pin
Anna-Jayne Metcalfe18-Apr-11 21:44
Anna-Jayne Metcalfe18-Apr-11 21:44 
GeneralRe: Alternatives Pin
Albert Holguin19-Apr-11 3:47
professionalAlbert Holguin19-Apr-11 3:47 
GeneralRe: Alternatives Pin
Anna-Jayne Metcalfe19-Apr-11 4:22
Anna-Jayne Metcalfe19-Apr-11 4:22 
QuestionRe: Alternatives Pin
Albert Holguin19-Apr-11 4:32
professionalAlbert Holguin19-Apr-11 4:32 
AnswerRe: Alternatives Pin
Anna-Jayne Metcalfe19-Apr-11 4:44
Anna-Jayne Metcalfe19-Apr-11 4:44 
GeneralRe: Alternatives Pin
Albert Holguin19-Apr-11 5:02
professionalAlbert Holguin19-Apr-11 5:02 
GeneralRe: Alternatives Pin
Anna-Jayne Metcalfe19-Apr-11 10:53
Anna-Jayne Metcalfe19-Apr-11 10:53 
GeneralRe: Alternatives Pin
Albert Holguin19-Apr-11 11:20
professionalAlbert Holguin19-Apr-11 11:20 
GeneralRe: Alternatives Pin
Anna-Jayne Metcalfe19-Apr-11 21:42
Anna-Jayne Metcalfe19-Apr-11 21:42 
GeneralRe: Alternatives Pin
Albert Holguin20-Apr-11 2:08
professionalAlbert Holguin20-Apr-11 2:08 
GeneralRe: Alternatives Pin
Anna-Jayne Metcalfe20-Apr-11 2:21
Anna-Jayne Metcalfe20-Apr-11 2: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.