It's my fourth day with <Accelerated C++> and I have to admit that this is WAY over my head.
I picked this book because of the sticky "C++ Books" but really... this was NOT meant for beginners. (or is it just me?)

Okay, enough of the whining.

By taking joeprogrammer's advice, I trashed VC++ 6.0 and got 8.0.
Unlike 6.0, 8.0 inserts stdafx.h to the project. (I learned that I can turn off this option but since it's suppose to save me some time while compiling -- even though I am no where close to creating such big projects that would benefit from a precompiled header -- I decided to not to.)

So there's main.cpp, stdafx.h, stdafx.cpp by defualt.
Since I'm trying to compile a cross reference table code using associative container map in chapter 7, I added files xref.h, xref.cpp, split.h and split.cpp.

When I pressed Ctrl+F5, the build failed and error C1010 showed up.

error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

After including stdafx.h to both xref.cpp and split.cpp it worked.

I've noticed that unlike other header files stdafx.h does not start with #ifndef. Then how is it safe to include the same header to both .cpp files?
Moreover, why does it need to be included in the first place? Isn't it enough to include it once?

The best thing you can do is to create a blank project or if your project already contains the stdafx files, copy the header information from them into your main file and delete them.

Since I don't have a MS Compiler with me right now, I can't verify the fact but you are good to go by writing your programs without those files, the way normal people do.

The best thing you can do is to create a blank project or if your project already contains the stdafx files, copy the header information from them into your main file and delete them.

Thanks for the help. From now on I'll just create blank projects without stdafx.h and stdafx.cpp.
However, I'm still curious about the way stdafx.h works and why it needs to be included to .cpp files in the project other than main.cpp.
Could anyone shed some light on this, please? It would be most appreciated.

Thanks for the help. From now on I'll just create blank projects without stdafx.h and stdafx.cpp.
However, I'm still curious about the way stdafx.h works and why it needs to be included to .cpp files in the project other than main.cpp.
Could anyone shed some light on this, please? It would be most appreciated.

stdafx.h is just a header file that contains other standard and non-changing header files that are used by most *.cpp programs. For example you might write a win32 api program that consists of 10 *.cpp files. Since they all contain windows.h, string, vector, and several others you can just put them all in stdafx.h. Then when the VC++ compiler begins the build process the first thing it does is preprocess everything in stdafx.cpp (which only includes stdafx.h) and saves that in a precompiled header file for use by the other 9 *.cpp files. This saves a great deal of compil time on large projects. For example, I started working on a medium sized project at work (about 100 or so *.cpp files). The original programmer turned off precompiled headers when he created the project. It took 45 minutes the first time I tried to compile the project with VC++ 2005. I spent a few minutes adding precompiled headers back in, and then it took only about 5 minutes to compile the project.

I've seen some programmers complain about precompiled headers because it causes all those header files to be included in all *.cpp files whether actually used or not. My answer -- So what? The compiler isn't as lazy as us humans and compilers today are smart enough to just toss out the extraneous stuff. I'm more interested in MY time spent watching the compiler do its thing then I am worry about such triviel things.

Microsoft is not the only one that supports precompiled headers -- I think GNU g++ does too.

This article has been dead for over six months. Start a new discussion instead.