| | |
Including stdafx.h in other .cpp files
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 5
Reputation:
Solved Threads: 0
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.
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?
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?
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.
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.
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
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
•
•
Join Date: Mar 2007
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
•
•
•
•
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.
![]() |
Similar Threads
- Precompiled Headers & Unexpected EOF error (C++)
- can't link to included files... y? (C++)
- problems wid cpp files (C++)
Other Threads in the C++ Forum
- Previous Thread: I/O in Soundcard and DTMF?
- Next Thread: Linked Lists Help C++
Views: 15160 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






