Using Header Files and StdAfx.h

I have a simple test console application. I am trying to use the nike2 function from the one defined in the simple.h file, and the implementation is in simple.cpp. Both simple.h and simple.cpp files are in different directories than the main project.

I added simple.h to the Header Files and simple.cpp to the Source Files in the project explorer (I'm not sure if this is necessary)

Console Application:

#include "stdafx.h" #include "..\..\simple.h" int _tmain(int argc, _TCHAR* argv[]) { nike2(5); return 0; } 

Simple.h

 #include <cstdlib> #include <iostream> #ifndef MEMORY #define MEMORY int var; int nike2(int f); #endif /*MEMORY*/ 

Simple.cpp

 #include <cstdlib> #include <iostream> #include "simple.h" int nike2(int f) { return 0; } 

During compilation, I got an error:

 Error 4 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source? c:\c\simple.cpp 11 1 usingHeader 

Why? What is the magic of "StdAfx.h" used for?

UPD

simple.cpp now looks like this, but still has the same error

 #include "C:\c\usingHeader\usingHeader\stdafx.h" #include <cstdlib> #include <iostream> #include "simple.h" int nike2(int f) { return 0; } 
+4
source share
3 answers

Stdafx.h used to create precompiled headers. It contains some of the most standard and commonly used include s.

This is mainly done to speed up the compilation process, because WinAPI is a very difficult thing.

You can also check this question , it has a more detailed answer.

+7
source

stdafx.h contains the header includes things that you do not expect to change. Things like standard libraries are included from stdafx.h, so they only need to be compiled once. You should include stdafx.h wherever you need it. If you do not want to use it, you can disable it in the project settings.

+2
source

What is the magic of "StdAfx.h" used for?

It is used for precompiled headers in Visual Studio.

You can include it in your .cpp files or select "Do not use precompiled headers" for those files that do not require all Windows headers.

+2
source

All Articles