C++ 错误 C1083:无法打开包含文件:'stdafx.h'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26330178/
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
Error C1083: Cannot open include file: 'stdafx.h'
提问by Kulis
When I compiled this program (from C++ Programming Language 4th edition):
当我编译这个程序时(来自 C++ Programming Language 4th edition):
main.cpp
主程序
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
向量.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
向量.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
It gave me these errors:
它给了我这些错误:
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?
我在 Windows 7 上的 Microsoft Visual Studio 2013 上运行它。如何修复它?
回答by ceztko
You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h
only for the _TCHAR
macro.
您必须正确理解什么是“stdafx.h”,也就是预编译头文件。其他问题或维基百科会回答这个问题。在许多情况下,可以避免使用预编译头文件,尤其是当您的项目很小且依赖项很少时。在您的情况下,因为您可能从模板项目开始,它Windows.h
仅用于包含_TCHAR
宏。
Then, precompiled header is usually a per-project file in Visual Studio world, so:
然后,预编译头通常是 Visual Studio 世界中的每个项目文件,因此:
- Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
- Change the
#include <stdafx.h>
to#include "stdafx.h"
. It is supposed to be a project local file, not to be resolved in include directories.
- 确保您的项目中有文件“stdafx.h”。如果您不这样做(例如您删除了它),只需创建一个新的临时项目并从那里复制默认项目;
- 将 更改
#include <stdafx.h>
为#include "stdafx.h"
。它应该是一个项目本地文件,而不是在包含目录中解析。
Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h
.
其次:不建议将预编译的头文件包含在您自己的头文件中,以免弄乱可以将您的代码用作库的其他源的命名空间,因此将其包含在vector.h
.
回答by user3718058
Just include windows.h instead of stdfax or create a clean project without template.
只需包含 windows.h 而不是 stdfax 或创建一个没有模板的干净项目。
回答by Dila Gurung
There are two solutions for it.
有两种解决方案。
Solution number one: 1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings... *** Do not check empty project)
解决方案一: 1.重新创建项目。创建项目时确保选中预编译头(应用程序设置... *** 不要检查空项目)
Solution Number two: 1.Create stdafx.h and stdafx.cpp in your project 2 Right click on project -> properties -> C/C++ -> Precompiled Headers 3.select precompiled header to create(/Yc) 4.Rebuild the solution
解决方案二:1.在你的项目中创建 stdafx.h 和 stdafx.cpp 2 右键单击项目 -> 属性 -> C/C++ -> Precompiled Headers 3.select precompiled header to create(/Yc) 4.Rebuild the solution
Drop me a message if you encounter any issue.
如果您遇到任何问题,请给我留言。
回答by Sar Jag
You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.
您可以通过将“$(ProjectDir)”(或 stdafx.h 所在的任何位置)添加到 Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories 下的目录列表来解决此问题。
回答by Dila Gurung
Add #include "afxwin.h"
in your source file. It will solve your issue.
添加#include "afxwin.h"
到您的源文件中。它将解决您的问题。
回答by Robert Morel
Just running through a Visual Studio Code tutorial and came across a similiar issue.
刚刚通过 Visual Studio Code 教程运行并遇到了类似的问题。
Replace #include "stdafx.h"
with #include "pch.h"
which is the updated name for the precompiled headers.
替换#include "stdafx.h"
为#include "pch.h"
which 是预编译头的更新名称。