编译错误:无法打开预编译头文件.pch -- 没有这个文件或目录。

8
我最近购买了Bjarne Stroustrup的《Programming: Principles and Practice Using C++》一书,并一直在跟进学习。目前我卡在了一个早期项目上,需要输出一些文本字符串。我使用Windows 10 Lenovo Yoga 2 Pro笔记本电脑上的Visual Studio Community 2015更新1进行编译,但遇到了以下错误:“无法打开预编译头文件:Debug\Finding the Upstairs Bathroom.pch':没有那个文件或目录”。该项目的名称恰当地命名为“Finding the Upstairs Bathroom.cpp”。以下是代码:
// I have the headers "stdafx.h" as well as this specific header lib 
// Bjarne Stroustrup created and which I had linked
// called "../../std_lib_facilities.h", which contains the standard C++ lib functions.

(#) define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS 1

int main()`// Main function`

{  
    cout << "Take the key out of your right pants pocket\n"; // Outputs string of text

    cout << "Unlock the front door of the house\n";

    cout << "Open the front door and step inside\n";

    cout << "Lock the front door using the key\n";

    cout << "Move up the stairs, taking them two at a time\n";

    cout << "Walk to the second door on the left and face it directly\n";

    cout << "Using the door handle, turn it and push the door forward\n";

    cout << "Enter the bathroom while leaving the door open behind you\n";

    keep_window_open();

    return 0;
}

我希望有人能够找出如何修复我的错误。

展示你的所有包含文件。 - ForeverStudent
2
我会关闭预编译头文件,或者学习如何使用它们。https://msdn.microsoft.com/zh-cn/library/b4w02hte.aspx - Robert Jacobs
我的包含文件有:#include<iostream> #include<fstream> #include<sstream> #include<cmath> #include<cstdlib> #include<string> #include<list> #include<vector> #include<algorithm> #include<stdexcept> - Rudis
1
可能是重复的问题:如何解决构建时缺少.pch文件的问题? - Eran
另外,你似乎正在使用MFC(已经有一段时间没有使用VS了,但我非常确定这是这种情况)。如果你想学习C ++,最好避免使用这样的框架,并使用标准库。 - Eran
我不使用MFC。我仅仅使用C++标准库。 - Rudis
3个回答

10

有两个解决方案:

  • 重新构建整个项目。这样应该可以(重新)创建预编译头文件(或者首先选择 Build/Clean Solution )。

  • 关闭预编译头文件。 项目设置 / C/C++ / 预编译头 / 不使用预编译头文件


我决定创建一个全新的项目,并做完全相同的事情,但这一次我没有预编译头文件。它成功编译了。感谢您的帮助。 - Rudis
@Rudis - 好的,但是Build -> Clean Solution也应该适用于您的其他项目(然后进行完整的重建)。 - Danny_ds
如果有一天我遇到类似的错误,我会记住这个。 - Rudis
加1以实现简洁的解决方案 - 注意:我已经创建了pch.h yc,但无论如何都存在这个问题,这就是“简洁”发挥作用的地方。 - ycomp

9

确保StdAfx.cpp>右键点击>属性(不是+enter)中有'Precompiled Header'='Create /Yc'

其他的.cpp文件应该有'Precompiled Header'='Use /Yu'


1
在一个大型的遗留项目上,经过数小时的烦恼,以上方法解决了我的问题。 - alman
1
这应该是所选的答案。目前所选的答案只是回避了问题。这个解决了问题,并解释了创建和使用之间的区别。 - M Katz

4
除了 Danny_ds 提出的选项外,您还可以:
  • 创建预编译头文件:项目设置 / C / C++ / 预编译头

    预编译头:创建 (/Yc)

    Create VS precompiled headers

  • 禁用预编译头文件:项目设置 / C / C++ / 预编译头

    预编译头:不使用预编译头文件

当我第一次需要创建预编译头文件时也遇到了这个问题。


我的问题是项目默认为使用而不是创建。 - Andreas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接