"stdafx.h"???

Started by
10 comments, last by Excors 17 years, 9 months ago
I had been using other compilers but recently i moved to vc++.net express edition. All went fine but when i go to just create a console application to test some stl things, it adds "stdafx.h" and it uses a very odd main definition instead of the standard. Why is it changing things around so much?
Advertisement
the stdafx.h is your precompiled header. You don't have to use it. Just turn it off in project settings C++/Precompiled headers.

I believe the main definition vs.net uses now is standards compliant. you might be looking at _tmain(..). That's a microsoft macro that expands to the Unicode or ansi version of main depending on which you compile under.

If you cretated a windowed app though you'd end up with winmain instead.

Cheers
Chris
CheersChris
Stdafx.h is the "precompiled header" file (well actually its the header file that the PCH is generated from).

Basically every C or CPP file in your project has to include this file as the first line in its file (unless you explicitally turn off precompilied headers for that file). All the includes listed in this file will be precompiled to a binary PCH file which speeds up compile time.

The file Stdafx.cpp is just stub that is tells .NET to compile Stdafx.h into a PCH file.

Its a pretty dumb system IMO, but it can really speed up compile times. Personally I generally don't use the automatic PCH generation feature (it never quite worked correctly for me, though I've not tried in 2005 it may be better). If you want to turn off completely and delete Stdafx.c/.h then the option is in properties->C/C++->Precompiled headers, change "Create/Use precompiled headers" to "not using precompiled headers".
Thanks a lot guys.
Quote:Original post by griffin2000
Its a pretty dumb system IMO, but it can really speed up compile times.


What don't you like about precompiled headers??

Cheers
Chris
CheersChris
also note it doesn't have to be named stdafx either... sometimes i name mines pch.h... if i recall right stdafx is just old-time MFC lingo for its precompiled headers...
what about the weird setup of main wheny ou create a new console project?
Quote:
What don't you like about precompiled headers??


Its not PCH per-se. Its the way they are implemented on .net is very crap. The automatic generation process is just busted as far as I can work out. You can get them to work by setting them up manually, but the mechanism is very clunky (the way it forces you to make the PCH header the first line in all your source files is very irritating).

Quote:Original post by griffin2000
Basically every C or CPP file in your project has to include this file as the first line in its file (unless you explicitally turn off precompilied headers for that file). All the includes listed in this file will be precompiled to a binary PCH file which speeds up compile time.


Quick note about adding the precompiled header to each c/cpp file. Visual studio has an option that will allow you to force an include on all of your files if you wish to use it. You can find it under project properties, c/c++->advanced. Enter in your .h file to the force includes and it should be good to go without explicitly adding the .h to each file.

I don't like how MS does PCH either. It breaks the ANSI standard, which is never a good compiler feature. Anything defined before you include the header is forgotten, and it forces an order to your includes. The PCH must come first. Take the same file, including the same headers, and defining the same defines, with PCH turned off, and it SHOULD compile the same, just slower. Not so in MSVC. It breaks the compile.

ie:

#define MAX_PLAYERS 4
#include "stdafx.h"
// MAX_PLAYERS has magically disappeared by here.

or

#if defined TOOL
#include "toolheader.h"
#elif defined GAME
#include "gameheader.h"
#endif
// forgets that it's inside #ifs. Compile breaks.


Also, while you CAN change it's name from stdafx.h, don't. Please, don't. Why? Because your cpp files MUST include it first. Have you made a nice AVLTree you'd like to use in multiple projects? If you call your header stdafx.h you can add a generic version into all the projects you want. If you name your header differently per project, you must make copies and edit them, or you must disable PCH for shared CPP files.

This topic is closed to new replies.

Advertisement