How to fix .pch file missing on build?
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.
- Right-click on your project in the Solution Explorer.
- Click Properties at the bottom of the drop-down menu.
- At the top left of the Properties Pages, select All Configurations from the drop-down menu.
- Open the C/C++ tree and select Precompiled Headers
- Precompiled Header: Select Use (/Yu)
- Fill in the Precompiled Header File field. Standard is stdafx.h
Click Okay
If you do not have stdafx.h in your Header Files put it there. Edit it to #include all the headers you want precompiled.
- Put a file named stdafx.cpp into your project. Put #include "stdafx.h" at the top of it, and nothing else.
- Right-click on stdafx.cpp in Solution Explorer. Select Properties and All configurations again as in step 4 ...
- ... but this time select Precompiled Header Create (/Yc). This will only bind to the one file stdafx.cpp.
- Put #include "stdafx.h" at the very top of all your source files.
Lucky 13. Cross your fingers and hit Build.
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.
Fix:
Make sure you have xxxxx.cpp in your project
Compile xxxxx.cpp with /Yc flag (Create Precompiled Header)
(right click on xxxxx.cpp -> properties -> Precompiled Headers -> create)Compile all other files with /Yu flag (Use Precompiled Header)
(right click on project -> properties -> Precompiled Headers -> use)
- Right click to the project and select the property menu item
- goto C/C++ -> Precompiled Headers
- Select Not Using Precompiled Headers
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.