C++ 如何修复构建时丢失的 .pch 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6096384/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:29:27  来源:igfitidea点击:

How to fix .pch file missing on build?

c++visual-studioprecompiled-headerspch

提问by Sam Mackrill

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?

当我在 Visual Studio 中构建我的 c++ 解决方案时,它抱怨 xxxxx.pch 文件丢失。是否有我缺少的设置来获取预编译的头文件?

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

回答by Jive Dadson

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.

注意:IDE 的更高版本可能会在相关文件的默认名称中使用“pch”而不是“stdafx”。在下面的说明中,可能需要用 pch 替换 stdafx。我道歉。这不是我的错。

  1. Right-click on your project in the Solution Explorer.
  2. Click Properties at the bottom of the drop-down menu.
  3. At the top left of the Properties Pages, select All Configurations from the drop-down menu.
  4. Open the C/C++ tree and select Precompiled Headers
  5. Precompiled Header: Select Use (/Yu)
  6. Fill in the Precompiled Header File field. Standard is stdafx.h
  7. Click Okay

  8. If you do not have stdafx.h in your Header Files put it there. Edit it to #include all the headers you want precompiled.

  9. Put a file named stdafx.cpp into your project. Put #include "stdafx.h" at the top of it, and nothing else.
  10. Right-click on stdafx.cpp in Solution Explorer. Select Properties and All configurations again as in step 4 ...
  11. ... but this time select Precompiled Header Create (/Yc).This will only bind to the one file stdafx.cpp.
  12. Put #include "stdafx.h" at the very top of all your source files.
  1. 在解决方案资源管理器中右键单击您的项目。
  2. 单击下拉菜单底部的属性。
  3. 在属性页面的左上角,从下拉菜单中选择所有配置。
  4. 打开 C/C++ 树并选择 Precompiled Headers
  5. 预编译头:选择使用 (/Yu)
  6. 填写预编译头文件字段。标准是 stdafx.h
  7. 单击确定

  8. 如果您的头文件中没有 stdafx.h,请将其放在那里。将其编辑为 #include 您想要预编译的所有头文件。

  9. 将名为 stdafx.cpp 的文件放入您的项目中。把 #include "stdafx.h" 放在它的顶部,没有别的。
  10. 在解决方案资源管理器中右键单击 stdafx.cpp。再次选择属性和所有配置,如步骤 4 ...
  11. ...但这次选择 Precompiled Header Create (/Yc)。这只会绑定到一个文件 stdafx.cpp。
  12. 将 #include "stdafx.h" 放在所有源文件的最顶部。

Lucky 13. Cross your fingers and hit Build.

幸运 13. 交叉手指并点击 Build。

回答by MSalters

Precompiled Header (pch) use is a two-step process.

预编译头 (pch) 的使用是一个两步过程。

In step one, you compile a stub file (In VS200x it's usually called stdafx.cpp. Newer versions use pch.cpp.). This stub file indirectly includes only the headers you want precompiled. Typically, one small header (usually stdafx.hor pch.hpp) lists standard headers such as <iostream>and <string>, and this is then included in the stub file. Compiling this creates the .pch file.

在第一步中,您编译一个存根文件(在 VS200x 中它通常被称为stdafx.cpp. 较新的版本使用pch.cpp.)。这个存根文件间接地只包括你想要预编译的头文件。通常,一个小标题(通常为stdafx.hpch.hpp)列出标准标题,例如<iostream><string>,然后将其包含在存根文件中。编译它会创建 .pch 文件。

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.

在第 2 步中,您的实际源代码包含第 1 步中与第一个标头相同的小标头。编译器在遇到这个特殊的头文件时,会改为读取相应的 .pch 文件。这意味着它不必每次都(重新)编译这些标准头文件。

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. If xxxxx.cppis present, and is such a stub file, then it's probably missing its /Yc:compiler option.

在您的情况下,第 1 步似乎失败了。存根文件是否仍然存在?在您的情况下,那可能是xxxxx.cpp. 它必须是一个用 编译的文件/Yc:xxxxx.pch,因为这是编译器标志,表明它是 PCH 过程的第 1 步。如果xxxxx.cpp存在,并且是这样的存根文件,则它可能缺少其/Yc:编译器选项。

回答by franckspike

Fix:

使固定:

  1. Make sure you have xxxxx.cpp in your project

  2. Compile xxxxx.cppwith /Yc flag (CreatePrecompiled Header)
    (right click on xxxxx.cpp -> properties -> Precompiled Headers -> create)

  3. Compile all other fileswith /Yu flag (UsePrecompiled Header)
    (right click on project -> properties -> Precompiled Headers -> use)

  1. 确保你的项目中有 xxxxx.cpp

  2. 使用 /Yc 标志编译xxxxx.cpp创建预编译头文件)
    (右键单击 xxxxx.cpp -> 属性 -> 预编译头文件 ->创建

  3. 使用 /Yu 标志编译所有其他文件使用预编译头文件)
    (右键单击项目 -> 属性 -> 预编译头文件 ->使用

回答by jpabess

  1. Right click to the project and select the property menu item
  2. goto C/C++ -> Precompiled Headers
  3. Select Not Using Precompiled Headers
  1. 右键单击项目并选择属性菜单项
  2. 转到 C/C++ -> 预编译头文件
  3. 选择不使用预编译头

回答by zar

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.

是的,它可以像其他人指出的那样使用 /Yc 选项消除,但很可能您不需要触摸它来修复它。为什么您在没有更改任何设置的情况下首先收到此错误?您可能已经“清理”了项目,而不是尝试编译单个 cpp 文件。在这种情况下,您会收到此错误,因为现在缺少预编译器头文件。只需构建整个项目(即使不成功),然后构建任何单个 cpp 文件,您就不会收到此错误。

回答by Marco Leite

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) 上发生这种情况,并且您使用 CocoaPods 确保您的 Podfile 已签入。

AppCenter only runs the "pod install" command if it finds a Pofile and it DOES NOT find the PODS folder on the files.

AppCenter 仅在找到 Pofile 并且在文件上找不到 PODS 文件夹时才运行“pod install”命令。

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.

我已签入该文件夹,但由于 git 自动忽略 .pch 文件(检查您 .gitignore 以确认这一点),我的 .pch 没有被签入。

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.

我通过强制 .pch 文件检查它来对我的问题进行排序,但删除 PODS 文件夹也应该起作用,因为在这种情况下 Appcenter 将运行 pod install 命令。

Hoppefully this helps somebody.

希望这有助于某人。

回答by Sergey ArcTechno

If everything is right, but this mistake is present, it need check next section in ****.vcxproj file:

如果一切正常,但出现此错误,则需要检查 ****.vcxproj 文件中的下一部分:

<ClCompile Include="stdafx.cpp">
  <PrecompiledHeader Condition=

In my case it there was an incorrect name of a configuration: only first word.

在我的情况下,配置的名称不正确:只有第一个词。

回答by glover12

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.

我知道这个话题很老,但我最近在 VS2015 中处理这个问题,有帮助的是删除构建文件夹并重新构建它。这可能是由于尝试关闭程序或程序在构建时暂停/冻结 VS 而发生的。

回答by Ahmed Awad

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 :)

我正在搜索有同样问题的 iOS PCH 文件,如果你也像我一样来到这里,我发现的解决方案是清除派生数据;关闭模拟器,转到 xCode 首选项 -> 位置 -> 转到派生数据文件路径,关闭 xCode,删除派生数据文件夹中的文件,重新启动并欢呼 :)

回答by Nick Dong

Try Build > Clean Solution, then Build > Build Solution. Thisworks for me.

尝试构建 > 清理解决方案,然后构建 > 构建解决方案。 对我有用。