Error: Cannot open source file "StdAfx.h" (Intellisense)


  • I am trying a test port of a VC++ project (ATL/WTL) from VC2005 to the 2010 release candidate.

    On opening some of my source files in the code editor I noticed a large number of Intellisense red error "squiggles" the first of which is "Error Cannot open source file "StdAfx.h"". The rest of them seem to be due to missing pre-compiled header symbols as would be expected. However dispite this the project builds and runs without any problem, so the compiler can find the precompiled header, but Intellisense cannot!

    I discovered that the files effected were all in a subdirectory off the main source directory. I tried replacing "#include "stdafx.h" with "#include "..\stdafx.h". This fixed the intellisense problem but now the file will not compile:

    Error 6 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?

    This after a number of warning C4627 including for "..\stdafx.h" (skipped when looking for precompiled header use). So it seems that Intellisense and the compiler use different search rules when looking for header files and that the compiler will only recognise the pre-compiled header file if it is specified exactly without a path.

    I did find a work-round for this bug: have two include lines as follows:

    #include "stdafx.h"
    #include "..\stdafx.h"

    Now both Intellisense and compiler work, with just the one Intellisense error squiggle under the first #include.

    Please let me know if there is a setting to fix this properly, although even if there is there is still a bug but it would then be in the project migration process.


    Saturday, May 01, 2010 10:41 AM

All replies

  • It looks like an Intellisense bug.  An other quicker fix is to add dummy StdAfx.h files in all source code sub-directories that reference the main stdafx.h.   Specifically the dummy stdafx.h will only have one line, such as:

    #include "..\StdAfx.h"

    These dummy files seem to keep Intellisense happy, and the compiler ignores them...
    Thursday, May 27, 2010 7:28 AM
  • The squiggle parser doesn't use the same pre-compiler header (PCH) mechanism as the compiler (cl.exe). The squiggle parser needs to find the actual header files. It relies on the include search path to do so. In this case, the squiggle parser fails to find stdafx.h  since the .cpp file is in a different directory from stdafx.h.

    You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Propertes->C/C++->General->Additional Include Directories.

    Also take a look at:

    http://msdn.microsoft.com/en-us/library/ms235519(VS.80).aspx  To approach IntelliSense failure in C++ projects/bullet 2.

    Tuesday, June 08, 2010 10:12 PM
  • Just had the same problem...I closed the project and reopened it and the problem dispeared !!!???
    Saturday, October 16, 2010 9:12 AM