8

I have recently purchased the text "Programming: Principles and Practice Using C++" by Bjarne Stroustrup and have been following it through. I am currently stuck on an early project, where I need to output some strings of text. Using Visual Studio Community 2015 update 1, on a Windows 10 Lenovo Yoga 2 Pro laptop, I have attempted to compile the project but have ran into an error detailed:

"Cannot open precompiled header file: Debug\Finding the Upstairs Bathroom.pch': No such file or directory". The name of the project is aptly named "Finding the Upstairs Bathroom.cpp". Here is the code:

// 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;
}

I would be grateful if someone is able to figure out how to fix my error.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
Rudis
  • 87
  • 1
  • 2
  • 7
  • show all of your includes – ForeverStudent Jan 14 '16 at 20:57
  • 2
    I would turn off pre compiled headers, or learn how to use them. https://msdn.microsoft.com/en-us/library/b4w02hte.aspx – Robert Jacobs Jan 14 '16 at 21:05
  • My includes are: #include #include #include #include #include #include #include #include #include #include – Rudis Jan 14 '16 at 21:17
  • 1
    Possible duplicate of [How to fix .pch file missing on build?](http://stackoverflow.com/questions/6096384/how-to-fix-pch-file-missing-on-build) – Eran Jan 14 '16 at 21:19
  • Also, you seem to be using MFC (haven't used VS for a while, but I'm pretty sure that's the case). If you'd like to learn C++, better avoid such frameworks, and make do with the standard library. – Eran Jan 14 '16 at 21:22
  • I'm not using MFC. I'm solely using the C++ standard library – Rudis Jan 14 '16 at 21:23

3 Answers3

10

There are 2 options to solve this:

  • Rebuild the full project. This should (re)create the precompiled header (or select Build/Clean Solution first).

  • Turn off precompiled headers. Project settings / C/C++ / Precompiled headers / Not Using Precompiled Headers.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • I decided to create a completely new project and do the exact same thing, but this time I did not precompile headers. It successfully compiled. Thankyou for the help. – Rudis Jan 14 '16 at 21:38
  • @Rudis - Ok, but `Build -> Clean Solution` should also work on your other project (and then a full rebuild). – Danny_ds Jan 14 '16 at 21:40
  • I will keep that in mind if I have an error similar to this one day – Rudis Jan 14 '16 at 21:42
  • plus 1 for clean solution - note: I already create pch.h yc and have this problem regardless that's where clean comes in – ycomp Jan 17 '23 at 13:04
6

Make sure StdAfx.cpp>right-click>Properties (not the same as +enter) has 'Precompiled Header'='Create /Yc'

The rest of the .cpp files should have 'Precompiled Header'='Use /Yu'

Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16
Stoyan Sabev
  • 61
  • 1
  • 2
  • After hours of grief on a large geacy project the above solved my problem. – alman Nov 14 '22 at 02:23
  • This should be the selected answer. The current selected answer only avoids the issue. This solves it and explains the difference between create and use. – M Katz Aug 04 '23 at 23:11
4

Appart from the options presented by Danny_ds, you can:

  • Create precompiled headers: Project settings / C/C++ / Precompiled Headers

    Precompiled Headers: Create (/Yc)

    Create VS precompiled headers

  • Disable precompiled headers. Project settings / C/C++ / Precompiled Headers

    Precompiled Headers: Not Using Precompiled Headers

I had the problem when I first had to create the precompiled header.

lmiguelmh
  • 3,074
  • 1
  • 37
  • 53