我可以使用 #include "pch.h" 吗?而不是 #include "stdafx.h";作为我在 Visual Studio C++ 中的预编译头文件?
这是我第一次使用 C++ 并正确地开始编码.我正在关注 learncpp.com 上的 C++ 教程,并且我正在使用 Visual Studio 2017 ...
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...
在教程中,他们从一些简单的Hello, world!"开始.在代码的顶部,他们将 #include "stdafx.h"
和 #include
放在一起.当我自己复制此代码并尝试构建它时,出现错误:
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:
C1010:查找预编译头时意外的文件结尾.您是否忘记将 #include "pch.h" 添加到您的源代码中?
C1010: unexpected end of file while looking for precompiled header. Did you forget to add #include "pch.h" to your source?
查看我的解决方案资源管理器时,我注意到在他们的教程中的头文件和源文件选项卡中,他们有名为stdafx.h"和stdafx.cpp"的文件,但对我来说这些文件称为pch.h"和pch.cpp".
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".
然后我尝试将 #include "stdafx.h"
重命名为 #include "pch.h"
并且代码被完美地构建和执行.那么我应该在其余代码中坚持使用 #include "pch.h"
还是这是某种问题?
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?
谢谢!
推荐答案
几年来,默认的预编译头文件名称是 stdafx.h.最近,在 VS 2017 中,他们将项目向导创建的默认名称更改为 pch.h.我不知道为什么,但他们做到了.
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.
可以在Configuration Properties -> C/C++ -> Precompiled Headers下的Project Properties中调整预编译头文件的名称.您还可以在那里启用或禁用它们.您甚至可以在每个文件的基础上定义它.您甚至可以在一个项目中配置多个不同的预编译头文件.
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.
所以对于你的具体问题:在你的教程中用 pch.h 替换 stdafx.h 不会有任何问题.该教程可能只是较旧,尚未更新.
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.
相关文章