将 C++ (.cpp) 编译成 .exe 文件

Compiling C++ (.cpp) into an .exe file

:) 帮助非常感谢。

我希望将 .cpp 文件转换为 .exe。

我试过 (MingW) gcc 和 g++。我也尝试在Visual Studio.

中手动编译它

不幸的是,这些无法编译(在 Linux 和 Windows 上),因为无法找到预编译的 header:

stdafx.h: No such file or directory

我尝试手动下载它们(预编译的 headers: #include)并将 #include <stdafx.h> 更改为 #include "stdafx.h" 以指向手动下载的预编译的当前文件夹header秒。这个 'works' 但它编译时出现大量错误,可能是因为它是 header 的错误版本(我唯一能找到 header 的地方是 https://sourceforge.net

一定有更简单的方法吗?

您可能使用了需要 stdafx.h.

的项目模板

要解决这个问题,您可以删除 cpp 文件中的 #include "stdafx.h"

或者,在使用 VS 模板创建新项目时关闭 Precompiled header 选项。例如,Win32 Console Application 模板:

stdafx.h 是用于 precompiled header files 的默认文件名。这是 Visual Studio 特定的优化,将很少更改的头文件编译为二进制表示,以加快编译速度。

预编译头文件的一个怪癖是,您必须包含用于 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) 编译器开关的头文件。

本质上,您的问题的解决方案是:

  1. 删除所有地方的 #include <stdafx.h> 指令。
  2. [可选] 在 Visual Studio 项目中启用预编译头文件的创建。
  3. 如果您选择在步骤 2 中创建预编译头文件,请为 Visual Studio 使用 /FI 编译器开关。