如何修复构建时缺少的 .pch 文件?

发布于 2024-11-09 11:39:31 字数 234 浏览 0 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

独﹏钓一江月 2024-11-16 11:39:31

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

  1. 在解决方案资源管理器中右键单击您的项目。
  2. 单击下拉菜单底部的属性。
  3. 在属性页的左上角,
    从下拉菜单中选择所有配置。
  4. 打开C/C++树并选择Precompiled Headers
  5. 预编译头:选择Use (/Yu)
  6. 填写Precompiled Header File字段。标准是 stdafx.h
  7. 单击“确定”

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

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

幸运 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.

  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.

Lucky 13. Cross your fingers and hit Build.

故人如初 2024-11-16 11:39:31

预编译头 (pch) 的使用分为两步。

第一步,编译一个存根文件(在 VS200x 中,它通常称为 stdafx.cpp。较新的版本使用 pch.cpp。)。此存根文件间接仅包含您想要预编译的标头。通常,一个小标头(通常是 stdafx.hpch.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 use pch.cpp.). This stub file indirectly includes only the headers you want precompiled. Typically, one small header (usually stdafx.h or 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.

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

橘寄 2024-11-16 11:39:31

修复:

  1. 确保项目中有 xxxxx.cpp

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

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

Fix:

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

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

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

夏日浅笑〃 2024-11-16 11:39:31
  1. 右键单击项目,选择属性菜单项
  2. goto C/C++ ->预编译头
  3. 选择不使用预编译头
  1. Right click to the project and select the property menu item
  2. goto C/C++ -> Precompiled Headers
  3. Select Not Using Precompiled Headers
感情洁癖 2024-11-16 11:39:31

是的,它可以像其他人指出的那样使用 /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.

南汐寒笙箫 2024-11-16 11:39:31

如果您在服务器构建 (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.

别忘他 2024-11-16 11:39:31

VS 搞砸了(我的是 2019 年;()。
继续并选择“不使用预编译头”,正如其他人指出的那样,然后使用任何文本编辑器打开项目文件(vcxproj),并删除两个位置中概述的两个条目。享受清理混乱的乐趣!
事实上,您在下面看到的 vcxproj 文件中的“pch.h”条目,您将在 VS 属性的界面中找到它。

快照来自nnn.vcxproj 文件

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.

snapshot from nnn.vcxproj file

过潦 2024-11-16 11:39:31

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

Try Build > Clean Solution, then Build > Build Solution. This works for me.

静谧 2024-11-16 11:39:31

我知道这个话题很老了,但我最近在 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.

谈场末日恋爱 2024-11-16 11:39:31

我正在寻找有同样问题的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 :)

烛影斜 2024-11-16 11:39:31

我设法为自己创建这个问题,因为我想使用不同目录中的 pch.h 和 pch.cpp 文件。因此,我从项目中删除了这两个文件,然后从其他地方将它们添加为现有文件。大错误,因为无法再找到预编译头文件。

我无法从 Visual Studio 2019 UI 中找到解决该问题的方法。您必须编辑项目文件并确保以下内容如下所示:

 <ClCompile Include="pch.cpp">
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
  </ClCompile>

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:

 <ClCompile Include="pch.cpp">
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
  </ClCompile>
坠似风落 2024-11-16 11:39:31

我遇到了同样的问题,我设法像这样解决它:

错误:

fatal error C1083: Cannot open precompiled header file : "Debug\myProj.pch". No such file or directory

第一个是当我遇到错误时,

并将其更改为第二张图片

  • ma​​ke (/Yx)

  • myProj.h
    输入图片此处描述

I had same issue, and I managed to solve it like this:

ERROR :

fatal error C1083: Cannot open precompiled header file : "Debug\myProj.pch". No such file or directory

first one is when I had an error,

and changed it like a second picture

  • make (/Yx)

  • myProj.h
    enter image description here

咽泪装欢 2024-11-16 11:39:31

就我而言,需要选择Create (/Yu),而不是标准的Use (/Yu)
输入图片此处描述

输入图片此处描述

In my case, it was necessary to select Create (/Yu), instead of the standard Use (/Yu)
enter image description here
to
enter image description here

网白 2024-11-16 11:39:31

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

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

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文