How to fix .pch file missing on build?

C++Visual StudioPrecompiled HeadersPch

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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)

Solution 4 - C++

  1. Right click to the project and select the property menu item
  2. goto C/C++ -> Precompiled Headers
  3. Select Not Using Precompiled Headers

Solution 5 - C++

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.

Solution 6 - C++

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.

Solution 7 - C++

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

Solution 8 - C++

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

Solution 9 - C++

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.

Solution 10 - C++

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

Solution 11 - C++

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>

Solution 12 - C++

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.

Solution 13 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSam MackrillView Question on Stackoverflow
Solution 1 - C++Jive DadsonView Answer on Stackoverflow
Solution 2 - C++MSaltersView Answer on Stackoverflow
Solution 3 - C++franckspikeView Answer on Stackoverflow
Solution 4 - C++jpabessView Answer on Stackoverflow
Solution 5 - C++zarView Answer on Stackoverflow
Solution 6 - C++Marco LeiteView Answer on Stackoverflow
Solution 7 - C++Sam SaarianView Answer on Stackoverflow
Solution 8 - C++Nick DongView Answer on Stackoverflow
Solution 9 - C++glover12View Answer on Stackoverflow
Solution 10 - C++Ahmed AwadView Answer on Stackoverflow
Solution 11 - C++Jack D MenendezView Answer on Stackoverflow
Solution 12 - C++Sergey ArcTechnoView Answer on Stackoverflow
Solution 13 - C++Zarina AbdibaitovaView Answer on Stackoverflow