Published by
Jun 18, 2013 (last update: Jun 18, 2013)

Use of "stdafx.h"

Score: 3.7/5 (277 votes)
*****
It is my first article please consider as a fresh article.

By definition "Stdafx.h" is a precompiled header.

Precompiled the word implies that this header file is precompiled (once compiled no need to compile it again).
Precompiled Header stdafx.h is basically used in Microsoft Visual Studio to let the compiler know the files that are once compiled and no need to compile it from scratch.

For Example:
If you are including the below windows header files
Code:
1
2
3
4
5
6
7
8
#include <windows.h>
#include <tchar.h>

int main()
{
 //your code
return 0;
}


The compiler would always compile this header files from scratch.
But if you include #include "stdafx.h" before this includes then the compiler will find the compiled header files from stdafx.h and does not compiled it from scratch. but if compiler doesn't find any compiled header files from stdafx.h then first it compiles that files and then stores its compiled version in stdafx.h. So that it can be use for next time compilation.

Code:
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>

int main()
{
//your code
 return 0;
}


What are its benefits:
Reduces compile time.
Reduces Unnecessary Processing.

So conclusion is use #include "stdafx.h" where you are actually using the other header files (like windows header files). Otherwise no need to use stdafx.h. And It doesn't mean that you remove it from your project but you may disable this precompiled header from project settings by selecting the file (in which you do not need this stdafx.h) and go to properties of it and find under C++ option -> Precompiled Header and select Use Precompiled Header to No.
That's it.
Enjoy :)