6

I have two include file headers

#include "stdafx.h"
#include "psapi.h"

However it gives a cannot open source file "stdafx.h" compile time error. I am using Visual Studios 2010. Is "stdafx.h" even necessary? I think so because the program cannot compile if i take it away.

0

2 Answers 2

8

Visual Studio uses it for "precompiled headers" feature. If you are not experienced with Visual Studio, I would recommend to keep the stdafx.h in the project.

And of course, if you #include it, you ought to have it.

Sign up to request clarification or add additional context in comments.

4 Comments

And if you include it, then you have to include it FIRST in every compilation unit. The preprocessor will IGNORE every leading line before encountering #include "stdafx.h". You can put anything before, no syntax error.
@Benoit: My Visual Studio (2010) reports something (at least a warning) when stdafx.h is not the first include.
Note: if any one file includes stdafx.h, then they all should. I had problems where only one file (from 3rd party) used it, so I had to include it in all my header files. I prefer not to use it. My projects aren't big enough to notice any time saved by precompiling; with Visual Studion, the hassle is not worth the savings.
@Benoit: Your comment should be an answer! I was puzzled for quite some time, when I encountered this error for I found no reasonable explanation! Thanks!
2

stdafx.h is used for precompiled headers. It is not necessary, but disabling the compiler feature can be a little tricky if you have never done it before. If you have it, then you must compile 'stdafx.cpp' before compiling anything else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.