Including stdafx.h in other .cpp files

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 5
Reputation: Earendil is an unknown quantity at this point 
Solved Threads: 0
Earendil Earendil is offline Offline
Newbie Poster

Including stdafx.h in other .cpp files

 
0
  #1
Mar 17th, 2007
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?
Last edited by Earendil; Mar 17th, 2007 at 2:24 pm.
There are 10 types of people in the world: Those who understand binary and those who don't.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Including stdafx.h in other .cpp files

 
0
  #2
Mar 17th, 2007
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 5
Reputation: Earendil is an unknown quantity at this point 
Solved Threads: 0
Earendil Earendil is offline Offline
Newbie Poster

Re: Including stdafx.h in other .cpp files

 
0
  #3
Mar 18th, 2007
Originally Posted by ~s.o.s~ View Post
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.
There are 10 types of people in the world: Those who understand binary and those who don't.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Including stdafx.h in other .cpp files

 
0
  #4
Mar 18th, 2007
it's VCs way of handling precompiled headers.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Including stdafx.h in other .cpp files

 
0
  #5
Mar 18th, 2007
Originally Posted by Earendil View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 15160 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC