tags:

views:

76

answers:

5

hello, i am new to windows c++ i have created an application in visual studio 2008 and i have created new win32 console and i have compiled an sample c++ program it asked me to add the stdafx header and is it is compulsory to add this header ??? i dont need any windows library....

+2  A: 

No, just create an EMPTY project, and add a main .cpp. This will be fine for any very small win32 projects. stdafx is absolutely unnessacery, and to be honest, I don't use it or even know what it is (I have only about a month experience with MSVC).

However, to make win32 applications, you need #include <Windows.h>, which is the windows header file.

Alexander Rafferty
+1 for empty project. That way you don't have to edit away the stuff that visual studio tries to help you with.
Default
I agree, I always like starting from scratch.
Alexander Rafferty
@Michael: +1 for the phrase "tries to help you with". ;-)
Thomas Matthews
+1  A: 

The wizard will assume that you want something called Precompiled Headers and generate your project based on this assumption. In the new project wizard, make sure that you click "next" rather than "finish" (or similar) and ask for an empty project. With that done, add a new .cpp file and write your code in it. You should no longer need to include stdafx.h in every source file.

Precompiled headers are quite useful in large C++ projects, in that they can significantly reduce compile time. However, for most small to mid-sized projects they are mostly an annoyance. I personally tend to create empty projects -- if I want PCH, I'll add them myself.

Martin Törnwall
actually i need to run the project in linux environment after production
sango
@sango: all the more reason to go with an empty project, then. I believe gcc can do precompiled headers, but I'm not sure if it plays well with the Visual C++ counterpart.
Martin Törnwall
@sango: Then you shouldn't use it. Either create a new project without pre-compiled headers or change the existing one to not to use them as some of the answers already describe.
sbi
+1  A: 

This file is not mandatory.

It is created by the Visual Studio Wizard, and has for purpose to be the general include file, that you'll include in all your cpp files, and that'll include all the header files you need. It will serve as being the precompiled header source.

Didier Trosset
+2  A: 

To be able to remove the file you have to disable Precompiled headers in project settings. This might increase compilation time, so I'd recommend you just familiarize yourself with the mechanism. Its described on Wikipedia and at MSDN

Crassy
+1  A: 

You don't need it, but compilation might break if you selected "use precompiled header" when you created your project and you don't turn off the corresponding "create/use precompiled header" compiler option.

A precompiled header is one that, as the name suggests, is partially pre-processed or compiled, so as to speed up the rest of compilation. Typically, if you were to use this feature, you'd put a bunch of #include statements in stdafx.h, for system headers that (virtually) all of the files in your project use. Things like <windows.h> are commonly put here, as are MFC or ATL GUI libraries. You can also put your own headers in there, but they should be ones that change only quite rarely.

Doug