3

If i use a normal text editor or Code::Blocks this problem didn't occur. but in visual studio , I have to include stdafx.h header file in order to compile the program without errors. I want to know whats the use of this header file and why its in visual studio. I'm using visual studio enterprise (2015).

Marleen Schilt
  • 650
  • 15
  • 26
  • That's the pre-compiled header. The idea is you drop all of the headers you need in stdafx.h, then it builds and caches one big file that can be used when compiling your program rather than having to hunt down and interpret all of the included headers (and headers included by those headers) every time you build. It can improve build times enormously. There should be a option in the project properties when you create the project to disable pre-compiled header use. – user4581301 Nov 19 '15 at 07:05
  • Here we go: http://stackoverflow.com/questions/7261707/how-to-avoid-precompiled-headers – user4581301 Nov 19 '15 at 07:06
  • "If i use a normal text editor or Code::Blocks this problem didn't occur." – no way. That's a non sequitur. This cannot possibly depend on the editor. This is a compilation problem. – The Paramagnetic Croissant Nov 19 '15 at 07:32
  • 1
    And almost a duplicate of http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio – user4581301 Nov 19 '15 at 07:48
  • I'm voting to close this question as off-topic because It takes 2 questions to answer, but both are answered. – user4581301 Nov 19 '15 at 07:50
  • I din't know about VS2015, but in VS2008 `"stdafx.h"` isn't required with proper project setting if my memory is correct. – MikeCAT Nov 19 '15 at 07:51
  • @user4581301 thank you; this problem wont happen if i start an empty project –  Nov 19 '15 at 10:14

1 Answers1

5

StdAfx.h is the default file name used to generate a Precompiled Header (see Creating Precompiled Header Files), when creating a standard project using Visual Studio's New Project wizard. Precompiled headers can significantly decrease the time it takes to compile a project by dumping a binary representation of parsed and pre-compiled headers. Usually, only rarely changing headers should be included in the precompiled header (Windows SDK headers, C++ Standard Library headers, CRT headers, etc.).

Use of precompiled headers is controlled through the /Y (Precompiled Headers) compiler switch. If you don't want to use precompiled headers, make sure neither /Yc (Create Precompiled Header File) nor /Yu (Use Precompiled Header File) is set in your project settings.

If you want to use precompiled headers, but do not want to (or can not) include the precompiled header file as the first include in all compilation units, you can employ the /FI (Name Forced Include File) compiler switch instead.

IInspectable
  • 46,945
  • 8
  • 85
  • 181