Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Including stdafx.h in a non-default location

4.00/5 (3 votes)
11 Apr 2011CPOL 46.1K  
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)