12
votes

So some reason, my .cpp file is missing it's header file. But I am not including the header file anywhere else. I just started so I checked all the files I made

enginuity.h

#ifndef _ENGINE_
#define _ENGINE_

class Enginuity
{

public:
    void InitWindow();

};

enginuity.cpp

#include "Enginuity.h"


void Enginuity::InitWindow()
{

}

main.cpp

#include "stdafx.h"
#include "GameProject1.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

code.....
#endif

dont know what's going on. The error I get is

1>c:\users\numerical25\desktop\intro todirectx\gameproject\gameproject1\gameproject1\enginuity.cpp(1) : warning C4627: '#include "Enginuity.h"': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\numerical25\desktop\intro todirectx\gameproject\gameproject1\gameproject1\enginuity.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?
11

11 Answers

34
votes

Did you read the error message?

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

I don't see an #include "stdafx.h" in enginuity.cpp. ;) If you're using precompiled headers, you need to include the precompiled header in every source (.cpp) file.

22
votes

I just experienced this error when including stdafx.h in a cpp file located in a parent folder above where stdafx.h is.

#include "subfolder\stdafx.h"

causes the compile error. Changing it to:

#include "stdafx.h"

fixes the compile error, but then intellisense freaks out.

The "fix" for intellisense, according to someone at Microsoft here, is to add "$(ProjectDir)" (or wherever the stdafx.h is) to the list of directories under Project->Properties->Configuration Propertes->C/C++->General->Additional Include Directories.

I've verified this works in Visual Studio 2012. Should work in 2010 as well.

6
votes

You'll either want to put the line

#include "stdafx.h"

at the top of all your .cpp files (in this case, enenuity.cpp is the only one missing it.

or disable precompiled headers in your project.

If you have precompiled headers enabled in your project, Visual C++ will look for that #include directive at the top of all your source files. If it's not there, you'll get the negative commentary you received.

5
votes

IF APPROVED SOLUTION DOES NOT WORK FOR YOU:

In my case the stdafx.h include was after other includes in my .cpp file.

Setting the #include "stdafx.h" statement at the TOP of the .cpp file fixed my errors.

3
votes

Your header file, enginuity.h is missing a #endif, or is there a mistake in the posting?

3
votes

It is possible to disable precompiled headers for a single file (VS2010). Select the .cc or .cpp file that is causing the annoyance, right mouse menu, properties, precompiled headers , Precompiled Header (change to) Not Using Precompiled Headers.

2
votes

Instead of adding #include "stdafx.h" to each .cpp file, you can use the Forced Include File feature. The feature is documented here: https://msdn.microsoft.com/en-us/library/8c5ztk84.aspx.

In my case, I was trying to compile existing .cpp files from another project in a new project. I knew that I shouldn't have to change each file because the original project had precompiled headers enabled and it was somehow compiling the same files successfully. I discovered that this option was configured in the original project, and the error went away after I made the same configuration in the new project.

1
votes

My issue was resolved when i mentioned #include "stdafx.h" on the top of all includes.

0
votes

Add #include "stdafx.h" to the top of enginuity.cpp or disable precompiled headers in your project.

0
votes

This error will occur if you have two solutions in a subdirectory of a project and precompiled headers are enabled. I have experienced the same problem and Microsoft has not fixed it yet.

https://connect.microsoft.com/VisualStudio/feedback/details/552449/

The microsoft work around sometimes works.

0
votes

As Viktor noted above including stdafx.h after other includes will also cause this error and can happen quite easily if you use the Add Function Wizard. In my case, the wizard added the #include to the trop of the cpp file (despite that it was already included) so I ended up with (using this example)

#include "Enginuity.h"
#include "stdafx.h"
// ... other includes ...
#include "Enginuity.h"