Is stdafx.h really necessary?

I see that many people using Visual C++ include <stdafx.h>. I understand it has something to do with precompiled headers but can't you use precompiled headers without including that ugly header?

To me it looks like a dirty trick by microsoft to make things less portable. I guess you can just provide an empty stdafx.h file when compiling on something else than Visual C++ but I still think it's ugly to have to include an extra header for things like this.
Last edited on
Is not necessary. The precompiled header file enhances compilation time significantly, but it is in no way mandatory unless you are using special project types, like ATL or MFC projects that don't allow you to deactivate the precompiled header.

But if you are programming only with the Windows API you can do what I do: New Win32 project, then select in the settings the project type and make sure you make it an empty project.

Then simply add a CPP file, add the main() or wWinMain() and go.
You could choose any name you want instead of stdafx.h and still use precompiled headers.

Or you could disable precompiled headers, no need to create an empty project.
It's "wizard" generated, that's why it's so common.

It is ugly. And while it's conventional in MFC apps, the wizard also generates it for non-MFC apps, which is really the problem.
If you want to know what the minimal is to create a console Win32 program, follow webJose's instructions above, paste this in a *.c or *.cpp file, and compile...

1
2
3
4
5
6
7
8
9
#include <cstdio>

int main(void)
{
 printf("Hello, World!\n");
 getchar();

 return 0;
}


No stdafx necessary. For a GUI program you could use WinMain() and just put up a MessageBox(). Personally, I allow no wizard generated code in my apps.
Topic archived. No new replies allowed.