致命错误:找不到’stdafx.h’文件

我是编程C ++的新手,我正在尝试通过网站(learncpp.com)学习自己,虽然我已经坚持编译我的第一个程序=(。他们使用Visual Studio编写代码,因为我使用的是macbook,我只是使用vi和终端(或者我应该使用其他东西吗?)

这是我根据教程编写的helloworld.cpp程序:

#include "stdafx.h" #include  { std::cout <<"Hello World!" <<std::end1; return 0; } 

当我编译(gcc -Wall hello.cpp)时,我收到错误:

 helloworld.cpp:1:10: fatal error: 'stdafx.h' file not found #include "stdafx.h" ^ 1 error generated. 

任何人都可以告诉我如何解决这个问题?

  1. stdafx.h是visual studio使用的预编译头,你不需要这个。
  2. 你似乎错过了int main()函数
  3. 它是std::endl而不是std::end1

所以这样的事情:

 #include  int main() { std::cout <<"Hello World!" < 

两个问题:a)不需要stdafx.h(正如其他人所说)。 b)’end1’应为’endl’(注意字母’l’与数字’1’)。

stdafx.h是一个预编译的头文件,它特定于Visual Studio。 除非您面临编译时间慢,否则预编译的头文件是没有价值的。 在你的程序中,你根本不需要它们,所以你可以删除它,一切都会好的。

您可能猜测是否不需要它们为什么包含它们?

我将解释它:每当我们添加头文件( #include )时,编译器将遍历它,检查它,然后在编译CPP文件时编译头文件。

对包含头文件的每个CPP文件重复此过程。

如果项目中有1000个CPP文件,必须说包含xyz.h头文件,那么编译器将编译xyz.h文件1000次。 这可能需要一段时间。

为了避免编译器为我们提供了“预编译”头文件的选项,因此它只会被编译一次以加快编译时间。