Im reading a tutorial at cplusplus.com and Ive got this code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!";
return 0;
}
And so I go to build and then build solution because im pretty sure thats what you have to do before compiling and I got this:
------ Build started: Project: example2, Configuration: Debug Win32 ------
Compiling...
example2.cpp
.\example2.cpp(8) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source
Build log was saved at "file://c:\Documents and Settings\Zach Williamson\My Documents\Visual Studio 2005\Projects\example2\example2\Debug\BuildLog.htm"
example2 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What am I doing wrong

Unexpected end of file problem
Ivo Manolov MSFT
Thanks, Reza. I'll re-install tonight and hopefully that'll sort it.
deepak2000
So is the only way to avoid these errors to simply not use PCHs I ask because I used to be able to use them fine, but a few weeks ago I began encountering this problem. I reset all VS settings to defaults, and it didn't change anything. Even the basic template apps encounter this same set of errors. I've spent several hours trying to figure out why this problem suddenly appeared on my machine, and I've finally given up on trying to use precompiled headers.
DeepakBakshani
wargammer2005
I have a project (sln) with several hpp header files, and several cpp files. + the main.cpp and the stdafx.h and stdafx.cpp.
when i first compiled, i got error 1010 Unexpected end of file etc. so i listened to the error and added "#include "stdafx.h" into every .cpp file, and it worked fine. Question: is that how its supposed to happen that EVERY cpp file has to include stdafx doesnt seem right...
so i tried not using PCH as was posted earlier, and i get 50 errors for each cpp file i removed "include stdafx" from, all errors complaining that none of my user-defined types are recognized. heres 104 errors from one of my files: http://rafb.net/paste/results/kaNdpN68.html
Zoner21
If the base template apps are having this problem, there is something wrong with your configuration. You may try reinstalling VS. Otherwise, when precompiled headers are enabled (and the pcheader is set to stdafx), you should be okay just including stdafx.h in each file.
-Reza
sadietz
MLynch
Thank you very much, this information also helped me. I've been struggling for three days, and someone suggested searching on Yahoo or Google for the answer. I found this site, and the information provided worked.
Thank you, Thank you, Thank you.
Tadeu
The Visual C++ help contains extensive help on almost every build error you receive, and how to solve each one. Whenever you receive a build error, it should be the first place you search for. To access it, click on the line which says:
.\example2.cpp(8) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source
), and then press F1. This will take you to the offline version of this page, which contains everything you want to know about error C1010.
That page tells you to:
Click on the Project Menu -> example2 Properties.
In the left pane, click the C/C++ folder.
Click the Precompiled Headers node.
Set this node to Not using Precompiled Headers.
Follow these steps and your error should be resolved.
B Turner
#include <iostream>
using namespace std;
int main() {
int i;
int b;
cout << "hello new user, my name is Zach. What is your name \n";
cin >> i; >> endl;
cout << "Please to meet you << i; << How old are you \n";
cin >> b;
cout << "That is very interesting!"
return 0;
}
And when I try to build the code it shows this problem:
------ Build started: Project: Zachs contants, Configuration: Debug Win32 ------
Compiling...
Zachs contants.cpp
.\Zachs contants.cpp(14) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source
Build log was saved at "file://c:\Documents and Settings\Zach Williamson\My Documents\Visual Studio 2005\Projects\Zachs contants\Zachs contants\Debug\BuildLog.htm"
Zachs contants - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The problem with this error is that I pressed F1 and it was talking about the #include code and Im confused. Is there a library I didnt include that I should have included
SQ
That is expected. When a project is created with precompiled headers, it is factoring out a lot of common definitions and headers into the stdafx. This needs to be included in every file for them to compile.
If you would like to manage the headers yourself, you can disable precompiled headers from the project and manually insert the necessary header files and definitions into each source file as needed. Personally, I prefer the precompiled headers, especially for MFC or ATL apps.
-Reza