0

:) Help greatly appreciated.

I am looking to convert a .cpp file to .exe.

I've tried with (MingW) gcc and g++. I also tried to manually compile it in Visual Studio.

Unfortunately these fail to compile (both on Linux and Windows) because the precompiled headers cannot be located:

stdafx.h: No such file or directory

I've tried to manually download them (the precompiled headers: #include) and change #include <stdafx.h> to #include "stdafx.h" to point at the current folder of the manually downloaded preheaders. This 'works' but it compiles with a massive amount of errors, probably because it's a wrong version of the header (Only place I could find the header was on https://sourceforge.net)

There must be a simpler way?

Community
  • 1
  • 1
Lykias
  • 13
  • 1
  • 4

2 Answers2

2

You probably used a project template that require stdafx.h.

To solve it you can either remove the #include "stdafx.h" in your cpp files .

Alternatively, toggle off Precompiled header option when creating new project using VS templates. For instance, Win32 Console Application template:

enter image description here

enter image description here

Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
  • 1
    I'm afraid that wasn't in question, the OP is very unclear though. – πάντα ῥεῖ Sep 20 '16 at 14:46
  • There is no connection between project templates and precompiled headers (other than you can enable/disable use of precompiled headers for **any** project type). There is nothing special about Win32 console projects in that respect. – IInspectable Sep 20 '16 at 20:28
  • Still bogus. No template *"requires"* use of precompiled headers. All templates **can** use precompiled headers. Precompiled headers **are** enabled by default for console applications, using the standard *Win32 Console Application* template (Visual Studio 2015). Doesn't sound like you are very comfortable with any of this. – IInspectable Sep 21 '16 at 16:31
1

stdafx.h is the default file name used for precompiled header files. This is Visual Studio specific optimization, that compiles rarely changing header files into a binary representation, to speed up compilation.

One quirk of precompiled header files is, that you have to include the header used to create the precompiled header file on the first non-comment/non-whitespace line in every compilation unit. Since this can become impractical (e.g. when using 3rd party libraries in source form), the compiler allows you to include the header file for you. The /FI (Name Forced Include File) compiler switch does just that.

In essence, the solution to your problem is:

  1. Remove the #include <stdafx.h> directive everywhere.
  2. [Optional] Enable creation of precompiled header files in the Visual Studio project.
  3. Use the /FI compiler switch for Visual Studio, in case you opted to create precompiled header files in step 2.
IInspectable
  • 46,945
  • 8
  • 85
  • 181