944,032 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 23656
  • C++ RSS
Mar 17th, 2007
0

Including stdafx.h in other .cpp files

Expand Post »
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.
Quote ...
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?
Last edited by Earendil; Mar 17th, 2007 at 2:24 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Earendil is offline Offline
5 posts
since Mar 2007
Mar 17th, 2007
0

Re: Including stdafx.h in other .cpp files

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 18th, 2007
0

Re: Including stdafx.h in other .cpp files

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Earendil is offline Offline
5 posts
since Mar 2007
Mar 18th, 2007
0

Re: Including stdafx.h in other .cpp files

it's VCs way of handling precompiled headers.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 18th, 2007
0

Re: Including stdafx.h in other .cpp files

Click to Expand / Collapse  Quote originally posted by Earendil ...
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.
Last edited by Ancient Dragon; Mar 18th, 2007 at 2:06 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I/O in Soundcard and DTMF?
Next Thread in C++ Forum Timeline: Linked Lists Help C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC