如何修复构建时缺少的 .pch 文件?
当我在 Visual Studio 中构建 C++ 解决方案时,它抱怨 xxxxx.pch 文件丢失。我是否缺少一个设置来获取预编译头?
这是完整性的确切错误:
Error 1 fatal error C1083: Cannot open precompiled header file: 'Debug\xxxxx.pch': No such file or directory
When I build my c++ solution in Visual Studio it complains that the xxxxx.pch file is missing. Is there a setting I am missing to get the pre-compiled headers back?
here is the exact error for completeness:
Error 1 fatal error C1083: Cannot open precompiled header file: 'Debug\xxxxx.pch': No such file or directory
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
注意:更高版本的 IDE 可能在相关文件的默认名称中使用“pch”而不是“stdafx”。在下面的说明中,可能需要用 pch 替换 stdafx。我道歉。这不是我的错。
从下拉菜单中选择所有配置。
单击“确定”
如果头文件中没有 stdafx.h,请将其放在那里。编辑
它将 #include 您想要预编译的所有标头。
在它的顶部,没有其他东西。
和所有配置再次如步骤 4 ...
绑定到一个文件 stdafx.cpp。
幸运 13. 交叉手指并点击“构建”。
NOTE: Later versions of the IDE may use "pch" rather than "stdafx" in the default names for related files. It may be necessary to substitute pch for stdafx in the instructions below. I apologize. It's not my fault.
select All Configurations from the drop-down menu.
Click Okay
If you do not have stdafx.h in your Header Files put it there. Edit
it to #include all the headers you want precompiled.
at the top of it, and nothing else.
and All configurations again as in step 4 ...
bind to the one file stdafx.cpp.
Lucky 13. Cross your fingers and hit Build.
预编译头 (pch) 的使用分为两步。
第一步,编译一个存根文件(在 VS200x 中,它通常称为
stdafx.cpp
。较新的版本使用pch.cpp
。)。此存根文件间接仅包含您想要预编译的标头。通常,一个小标头(通常是stdafx.h
或pch.hpp
)会列出标准标头,例如
和< ;string>
,然后将其包含在存根文件中。编译此文件将创建 .pch 文件。在步骤 2 中,您的实际源代码包含与步骤 1 中的第一个标头相同的小标头。当编译器遇到这个特殊头文件时,它会读取相应的 .pch 文件。这意味着它不必每次都(重新)编译这些标准头。
就你的情况而言,步骤 1 似乎失败了。存根文件是否仍然存在?在您的情况下,这可能是
xxxxx.cpp
。它必须是使用/Yc:xxxxx.pch
编译的文件,因为这是指示它是 PCH 过程的第 1 步的编译器标志。如果 xxxxx.cpp 存在,并且是这样的存根文件,那么它可能缺少其/Yc:
编译器选项。Precompiled Header (pch) use is a two-step process.
In step one, you compile a stub file (In VS200x it's usually called
stdafx.cpp
. Newer versions usepch.cpp
.). This stub file indirectly includes only the headers you want precompiled. Typically, one small header (usuallystdafx.h
orpch.hpp
) lists standard headers such as<iostream>
and<string>
, and this is then included in the stub file. Compiling this creates the .pch file.In step 2, your actual source code includes the same small header from step 1 as the first header. The compiler, when it encounters this special header, reads the corresponding .pch file instead. That means it doesn't have to (re)compile those standard headers every time.
In your case, it seems step 1 fails. Is the stub file still present? In your case, that would probably be
xxxxx.cpp
. It must be a file that's compiled with/Yc:xxxxx.pch
, since that's the compiler flag to indicate it's step 1 of the PCH process. Ifxxxxx.cpp
is present, and is such a stub file, then it's probably missing its/Yc:
compiler option.修复:
确保项目中有 xxxxx.cpp
使用 /Yc 标志编译 xxxxx.cpp (创建预编译头)
(右键单击 xxxxx.cpp -> 属性 -> 预编译头文件 -> 创建)
使用 /Yu 标志编译所有其他文件 (使用预编译头)
(右键单击项目 -> 属性 -> 预编译头 -> 使用)
Fix:
Make sure you have xxxxx.cpp in your project
Compile xxxxx.cpp with /Yc flag (Create Precompiled Header)
(right click on xxxxx.cpp -> properties -> Precompiled Headers -> create)
Compile all other files with /Yu flag (Use Precompiled Header)
(right click on project -> properties -> Precompiled Headers -> use)
是的,它可以像其他人指出的那样使用 /Yc 选项来消除,但很可能您不需要碰它来修复它。为什么在不更改任何设置的情况下首先出现此错误?您可能已经“清理”了该项目,然后尝试编译单个 cpp 文件。在这种情况下,您会收到此错误,因为现在缺少预编译器标头。只需构建整个项目(即使不成功),然后构建任何单个 cpp 文件,就不会出现此错误。
Yes it can be eliminated with the /Yc options like others have pointed out but most likely you wouldn't need to touch it to fix it. Why are you getting this error in the first place without changing any settings? You might have 'cleaned' the project and than try to compile a single cpp file. You would get this error in that case because the precompiler header is now missing. Just build the whole project (even if unsuccessful) and than build any single cpp file and you won't get this error.
如果您在服务器构建 (AppCenter) 上发生这种情况,并且您使用 CocoaPods,请确保您的 Podfile 已签入。AppCenter
仅在找到 Pofile 并且在上找不到 PODS 文件夹时才运行“pod install”命令文件。
我已经签入了该文件夹,但是因为 git 自动忽略 .pch 文件(请检查 .gitignore 来验证这一点),我的 .pch 没有被签入。
我通过强制 .pch 文件检查它来解决我的问题,但删除 PODS 文件夹也应该有效,因为在这种情况下 Appcenter 将运行 pod install 命令。
希望这对某人有帮助。
In case this is happening to you on a server build (AppCenter) and yo uaer using CocoaPods ensure that your Podfile is checked in.
AppCenter only runs the "pod install" command if it finds a Pofile and it DOES NOT find the PODS folder on the files.
I had the folder checked-in, but because git automatically ignores .pch files (check you .gitignore to veryfy this), my .pch weren'nt being checked in.
I sorted my issue by forcing the .pch files to check it, but Deleting the PODS folder should work too, since Appcenter will run the pod install command in that case.
Hoppefully this helps somebody.
VS 搞砸了(我的是 2019 年;()。
继续并选择“不使用预编译头”,正如其他人指出的那样,然后使用任何文本编辑器打开项目文件(vcxproj),并删除两个位置中概述的两个条目。享受清理混乱的乐趣!
事实上,您在下面看到的 vcxproj 文件中的“pch.h”条目,您将在 VS 属性的界面中找到它。
VS screwed (mine is 2019 ;( ).
Go ahead and choose "not using precompiled headers" as other guys are pointing out then open the project file (vcxproj) with any text editor, and delete the outlined two entries in two places. Enjoy cleaning up the mess!
As a matter of fact, the 'pch.h' entry in the vcxproj file you see it below, you will ever find it in VS properties' interfaces.
尝试构建>清理解决方案,然后构建>构建解决方案。 这对我有用。
Try Build > Clean Solution, then Build > Build Solution. This works for me.
我知道这个话题很老了,但我最近在 VS2015 中处理这个问题,有帮助的是删除构建文件夹并重新构建它。发生这种情况的原因可能是尝试关闭程序或程序在构建时停止/冻结 VS。
I know this topic is very old, but I was dealing with this in VS2015 recently and what helped was to deleted the build folders and re-build it. This may have happen due to trying to close the program or a program halting/freezing VS while building.
我正在寻找有同样问题的iOS PCH文件,如果你也和我一样,我找到的解决方案是清除派生数据;关闭模拟器,转到 xCode prefs ->地点 ->转到派生数据文件路径,关闭xCode,删除派生数据文件夹中的文件,重新启动,欢呼:)
I was searching for the iOS PCH file having the same problem, if you got here like me too, the solution that I've found is by clearing derived data; Close Simulator(s), go to xCode prefs -> locations -> go to the derived data file path, close xCode, delete the files in the derived data folder, re launch and cheers :)
我设法为自己创建这个问题,因为我想使用不同目录中的 pch.h 和 pch.cpp 文件。因此,我从项目中删除了这两个文件,然后从其他地方将它们添加为现有文件。大错误,因为无法再找到预编译头文件。
我无法从 Visual Studio 2019 UI 中找到解决该问题的方法。您必须编辑项目文件并确保以下内容如下所示:
I managed to create this problem for myself because I wanted to use a pch.h and pch.cpp file from different directories. So, I deleted the two files from my project and then added them as existing files from somewhere else. Big mistake as precompiled header files can no longer be found.
There is no way that I can find to fix the problem from the Visual Studio 2019 UI. You must edit the project file and make sure the following look like this:
我遇到了同样的问题,我设法像这样解决它:
错误:
第一个是当我遇到错误时,
并将其更改为第二张图片
make (/Yx)
myProj.h
I had same issue, and I managed to solve it like this:
ERROR :
first one is when I had an error,
and changed it like a second picture
make (/Yx)
myProj.h
就我而言,需要选择
Create (/Yu)
,而不是标准的Use (/Yu)
到
In my case, it was necessary to select
Create (/Yu)
, instead of the standardUse (/Yu)
to
如果一切正常,但存在此错误,则需要检查 ****.vcxproj 文件中的下一部分:
在我的情况下,配置名称不正确:只有第一个单词。
If everything is right, but this mistake is present, it need check next section in ****.vcxproj file:
In my case it there was an incorrect name of a configuration: only first word.