C++ 致命错误:找不到“stdafx.h”文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22621928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: 'stdafx.h' file not found
提问by HymanieZ3895
I am new to programming C++ and am trying to learn myself through websites (learncpp.com) although I am already stuck on compiling my first program =( . They use Visual Studio to program their code and because I am using a macbook, I just use vi and terminal (or should I use something else?)
我是 C++ 编程新手,我正在尝试通过网站 (learncpp.com) 学习自己,尽管我已经坚持编译我的第一个程序 =(。他们使用 Visual Studio 来编程他们的代码,因为我使用的是 macbook,我只是使用 vi 和终端(或者我应该使用其他东西吗?)
Here's the helloworld.cpp program I wrote based on the tutorial:
下面是我根据教程写的helloworld.cpp程序:
#include "stdafx.h"
#include <iostream>
{
std::cout <<"Hello World!" <<std::end1;
return 0;
}
when I compiled (gcc -Wall hello.cpp) I get the error :
当我编译 (gcc -Wall hello.cpp) 时出现错误:
helloworld.cpp:1:10: fatal error: 'stdafx.h' file not found
#include "stdafx.h"
^
1 error generated.
Can anyone give me insight on to how to fix this?
谁能让我深入了解如何解决这个问题?
回答by Chris Drew
stdafx.h
is the precompiled header used by visual studio, you do not need this.- You seem to have missed out the
int main()
function - It is
std::endl
notstd::end1
stdafx.h
是visual studio 使用的预编译头文件,你不需要这个。- 你好像错过了这个
int main()
功能 - 它
std::endl
不是std::end1
So something like this:
所以像这样:
#include <iostream>
int main() {
std::cout <<"Hello World!" <<std::endl;
return 0;
}
回答by Jayesh Baviskar
stdafx.h
is a Precompiled Header file and it is specific to the Visual Studio.
Precompiled Header file is worthless unless you are facing slow compilation Time. In your program, you don't need them at all, so you can remove that and everything will be fine.
stdafx.h
是一个预编译头文件,它特定于 Visual Studio。除非您面临缓慢的编译时间,否则预编译的头文件毫无价值。在你的程序中,你根本不需要它们,所以你可以删除它,一切都会好起来的。
You might be guessing if it is not needed then why we include them?
您可能会猜测,如果不需要它,那么为什么我们要包含它们?
I will Explain it:
Whenever we add header files (#include
), The compiler will walk through it, Examine it and then compile the header file whenever CPP file is compiled.
我会解释一下:每当我们添加头文件(#include
)时,编译器都会遍历它,检查它,然后在编译 CPP 文件时编译头文件。
This process is repeated for each and every CPP file that has header file included.
对包含头文件的每个 CPP 文件重复此过程。
In case if you have 1000 CPP files in a project which has to say xyz.h header file included then the compiler will compile xyz.h
file 1000 times. it may take a noticeable time.
如果项目中有 1000 个 CPP 文件并且必须包含 xyz.h 头文件,那么编译器将编译xyz.h
文件 1000 次。这可能需要很长时间。
To avoid that compiler gives us the option to "precompile" the header file so it will get compiled only once to speed up the compilation time.
为了避免该编译器为我们提供了“预编译”头文件的选项,因此它只会编译一次以加快编译时间。
回答by Andy
Two problems: a) stdafx.h is not needed (as others noted). b) 'end1' should be 'endl' (note the letter 'l' vs. the number '1').
两个问题:a) 不需要 stdafx.h(正如其他人指出的那样)。b) 'end1' 应该是 'endl'(注意字母 'l' 与数字 '1')。