4

Ihave simple test console application. I'm trying to use function nike2 from defined in simple.h file and realization is in simple.cpp. Both simple.h and simple.cpp files are in different directory than main project.

I have added simple.h to "Header files" and simple.cpp to "Source files" in project explorer (I'm not sure it is needed)

Console app:

#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;
}

While compile I got 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 magic "StdAfx.h" used for?

UPD

simple.cpp now looks this way, but still have the same error

#include "C:\c\usingHeader\usingHeader\stdafx.h"
#include <cstdlib>
#include <iostream>
#include "simple.h"


int  nike2(int f)
{
return 0;
}
vico
  • 17,051
  • 45
  • 159
  • 315
  • 1
    ´I have added simple.h to Header files and simple.cpp to Source files in project explorer (I'm not sure it is needed)´ No, it isn't necessary, the Header and Source filters are only visualization filters and does not match any real-directory pattern of functional pattern it is only visualization and classification into visual studio IDE. BTW: I thinkt you dont really need "StdAfx". – PaperBirdMaster Aug 07 '12 at 14:09
  • In case I will need StdAfx, I will need to add it include it to simple.cpp? To make comiler happy I must write somerthing like `#include "C:\c\usingHeader\usingHeader\stdafx.h"` ? – vico Aug 07 '12 at 14:28
  • @user No, just add `#include "stdafx.h"` like the message shows. The file will be in your project directory, and doesn't need a full path. – Bo Persson Aug 07 '12 at 15:36

3 Answers3

7

Stdafx.h is used for making pre-compiled headers. It contains some of the most standard and commonly used includes.

That is made mainly to speed-up the compilation process, because WinAPI is a very heavy thing.

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

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • I have added #include `C:\c\usingHeader\usingHeader\stdafx.h` to simple.cpp , but nothing changed - still have this error. – vico Aug 07 '12 at 14:36
2

stdafx.h contains header includes for things that you aren't expecting to change. Things like standard libraries are included from stdafx.h so they only have to be compiled once. You should include stdafx.h everywhere you need it. If you would rather not use it, you can turn it off in the project preferences.

Samuel
  • 16,923
  • 6
  • 62
  • 75
  • I would not recommend disabling using of precompiled headers on Visual Studio as it's speeds up compiling so much. Even if you include in stdafx.h the std used headers and you get a lot of speedup for that only – Ghita Nov 28 '12 at 20:03
2

What is magic "StdAfx.h" used for?

It is used for precompiled headers in Visual Studio.

You can either include it in your .cpp files, or select "Not using precompiled headers" for those file that don't need all the Windows headers included.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203