我可以使用 #include "pch.h" 而不是 #include "stdafx.h" 作为我在 Visual Studio C++ 中的预编译头文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51928685/
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
Can I use #include "pch.h" instead of #include "stdafx.h" as my precompile header in Visual Studio C++?
提问by Honesty_DEV
This is my first time working with C++ and properly getting into coding. I'm following the C++ tutorials on learncpp.com and I'm using Visual Studio 2017...
这是我第一次使用 C++ 并正确地开始编码。我正在关注 learncpp.com 上的 C++ 教程,并且我正在使用 Visual Studio 2017 ...
In the tutorial they start off with some simple "Hello, world!" code and at top of the code they put #include "stdafx.h"
along with #include <iostream>
. When I replicate this code myself and try to build it, I get the error:
在教程中,他们从一些简单的“Hello, world!”开始。代码和他们放在#include "stdafx.h"
一起的代码的顶部#include <iostream>
。当我自己复制此代码并尝试构建它时,出现错误:
C1010: unexpected end of file while looking for precompiled header. Did you forget to add #include "pch.h" to your source?
C1010: 查找预编译头时文件意外结束。您是否忘记将 #include "pch.h" 添加到您的源代码中?
When looking at my solution explorer I noticed that in their tutorial in the header and source files tab they have files called "stdafx.h" and "stdafx.cpp", but for me those files are called "pch.h" and "pch.cpp".
在查看我的解决方案资源管理器时,我注意到在头文件和源文件选项卡中的教程中,他们有名为“stdafx.h”和“stdafx.cpp”的文件,但对我而言,这些文件称为“pch.h”和“pch” .cpp”。
So I then tried renaming the #include "stdafx.h"
to #include "pch.h"
and the code was built and executed perfectly. So should I just stick with #include "pch.h"
for the rest of my code or is this some kind of issue?
所以我然后尝试重命名#include "stdafx.h"
to#include "pch.h"
并且代码被完美地构建和执行。那么我应该坚持我#include "pch.h"
的其余代码还是这是某种问题?
Thanks!
谢谢!
回答by marc40000
The default precompiled headers name was stdafx.h for several years now. Lately, with VS 2017, they changed the default name the project wizard creates to pch.h. I don't know why but they did it.
几年来,默认的预编译头文件名称是 stdafx.h。最近,在 VS 2017 中,他们将项目向导创建的默认名称更改为 pch.h。我不知道为什么,但他们做到了。
You can adjust the name of the precompiled header file in the Project Properties under Configuration Properties -> C/C++ -> Precompiled Headers. You can also enable or disable the use of them there. You can even define this on a per file basis. You can even configure multiple different Precompiled Header files in one project.
可以在Configuration Properties -> C/C++ -> Precompiled Headers下的Project Properties中调整预编译头文件的名称。您还可以在那里启用或禁用它们。您甚至可以在每个文件的基础上定义它。您甚至可以在一个项目中配置多个不同的预编译头文件。
So for you concrete question: There won't be any kind of issue replacing stdafx.h with pch.h in your tutorial. The tutorial is probably just older and hasn't been updated yet.
所以对于你的具体问题:在你的教程中用 pch.h 替换 stdafx.h 不会有任何问题。该教程可能只是较旧,尚未更新。
回答by user3579754
Try moving "#include pch.h" to the very top of the file (before any other headers).
尝试将“#include pch.h”移动到文件的最顶部(在任何其他标题之前)。
Some compilers won't compile content before the pre-compiled header file(s) by default. This setting can be changed though.
默认情况下,某些编译器不会在预编译头文件之前编译内容。但是可以更改此设置。
回答by ManuAlvarado22
In the learncpp.com tutorials, the instructor(s) recommend(s) us to turn off the "Precompiled Header" additional option when creating a new C++ project using the Windows Desktop Wizard option on Visual Studio. And it is a good idea to do so while we haven't been introduced to Header files.
在 learncpp.com 教程中,讲师建议我们在使用 Visual Studio 上的 Windows 桌面向导选项创建新的 C++ 项目时关闭“预编译头”附加选项。在我们还没有介绍头文件的情况下这样做是个好主意。
However, pch.h seems to be the "new" stdafx.h, so it is not that you can use the former instead of the latter, but that you have to, at least if choosing the option of "Precompiled Header".
但是,pch.h 似乎是“新的”stdafx.h,因此并不是您可以使用前者代替后者,而是您必须这样做,至少在选择“预编译头”选项时是这样。