C++ 如何避免预编译头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7261707/
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
How to avoid precompiled headers
提问by Howdy_McGee
I am trying to compile a simple VS program in C++ as an assignment for class. We only ever include <iostream>
and I keep getting this error:
我正在尝试用 C++ 编译一个简单的 VS 程序作为类的分配。我们只包含<iostream>
并且我不断收到此错误:
1>Assignment.cpp(15): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add
'#include "StdAfx.h"'
to your source?
1>Assignment.cpp(15): 致命错误 C1010: 查找预编译头文件时意外结束。您是否忘记添加
'#include "StdAfx.h"'
到您的来源?
My program is literally this small...
我的程序真的是这么小...
#include <iostream>
using namespace std;
int main()
{
unsigned int day = 30;
cout << "My Name is John Doe" << endl;
cout << "My Major is CS" << endl;
cout << "I was born on day " << day << endl;
return 0;
}
I just installed Visual Studio Express 2010. Really I would love to start an empty project instead of installing with all these files predefined, I think it would make it a lot easier but I never get that option when creating a project. Anybody have any suggestions?
我刚刚安装了 Visual Studio Express 2010。我真的很想启动一个空项目,而不是安装所有这些预定义的文件,我认为这会让它变得更容易,但我在创建项目时从来没有得到这个选项。有人有什么建议吗?
回答by André Caron
You can always disable the use of pre-compiled headers in the project settings.
您始终可以在项目设置中禁用预编译头文件的使用。
Instructions for VS 2010 (should be similar for other versions of VS):
VS 2010 的说明(其他版本的 VS 应该类似):
Select your project, use the "Project -> Properties" menu and go to the "Configuration Properties -> C/C++ -> Precompiled Headers" section, then change the "Precompiled Header" setting to "Not Using Precompiled Headers" option.
选择您的项目,使用“Project -> Properties”菜单并转到“Configuration Properties -> C/C++ -> Precompiled Headers”部分,然后将“Precompiled Header”设置更改为“Not Using Precompiled Headers”选项。
If you are only trying to setup a minimal Visual Studio project for simple C++ command-line programs (such as those developed in introductory C++ programming classes), you can create an empty C++ project.
如果您只想为简单的 C++ 命令行程序(例如在介绍性 C++ 编程类中开发的程序)设置最小的 Visual Studio 项目,则可以创建一个空的 C++ 项目。
回答by Michael Burr
You can create an empty project by selecting the "Empty Project" from the "General" group of Visual C++ projects (maybe that project template isn't included in Express?).
您可以通过从 Visual C++ 项目的“常规”组中选择“空项目”来创建一个空项目(也许 Express 中不包含该项目模板?)。
To fix the problem in the project you already have, open the project properties and navigate to:
要解决您已有项目中的问题,请打开项目属性并导航至:
Configuration Properties | C/C++ | Precompiled Headers
And choose "Not using Precompiled Headers" for the "Precompiled Header" option.
并为“预编译头”选项选择“不使用预编译头”。
回答by hamstergene
The .cpp
file is configured to use precompiled header, therefore it must be included first (before iostream). For Visual Studio, it's name is usually "stdafx.h".
该.cpp
文件被配置为使用预编译头,因此必须首先包含它(在 iostream 之前)。对于 Visual Studio,它的名称通常是“stdafx.h”。
If there are no stdafx* files in your project, you need to go to this file's options and set it as “Not using precompiled headers”.
如果您的项目中没有 stdafx* 文件,您需要转到该文件的选项并将其设置为“不使用预编译头”。
回答by SergeyT
try to add #include "stdafx.h"
before #include "iostream"
尝试添加#include "stdafx.h"
之前#include "iostream"