Practical guide Visual Studio C++
Precompiled headers in Visual Studio
A precompiled header saves compiled state for a set of headers, which compatible source files can reuse. Each build configuration needs compatible compiler options and its own output. This can reduce repeated header parsing.
Visual Studio property-page paths describe MSBuild projects. For CMake, configure the corresponding target or preset. Menu names can vary by Visual Studio version; use the cited reference for your toolset.
Transcript
Precompiled headers set up by CppPilot in a C++ project in Visual Studio. Catalog has eleven source files, and each one parses the same standard headers again.
Choose Extensions > CppPilot > Project & Solution > Build Performance and Modernization. CppPilot reads the project settings for every configuration, and the AI checks what would save build time.
One finding: no precompiled header. Set Precompiled Header to Use, name a common header, and let one source file create it.
Click Fix review findings. The AI creates pch.h with the shared standard headers and pch.cpp, which builds it, then sets the project to use it in every file.
Build again: the headers are compiled once, then reused by the eleven files. CppPilot explains and fixes C++ build errors in Visual Studio.
- pch.hStable, shared headers
- pch.cpp /YcCreates the .pch file
- app.cpp /YuReuses the compiled headers
The /Yc source creates a .pch file. Sources using /Yu reuse that file within the matching build configuration.
Set it up
- Create
pch.hwith the stable headers the project uses most, for example<windows.h>,<string>,<vector>, and third-party libraries. - Create
pch.cppthat contains only#include "pch.h". - In the project properties, for all configurations, set C/C++ > Precompiled Headers > Precompiled
Header to Use (/Yu) and Precompiled Header File to
pch.h. - In the properties of
pch.cpponly, set Precompiled Header to Create (/Yc). - Make
#include "pch.h"the first line of every.cppfile.
New Visual Studio projects come with this already done. Older projects name the file stdafx.h; it works the
same way.
pch.cpp — create (/Yc)
#include "pch.h"// This source creates the precompiled header.app.cpp — use (/Yu)
#include "pch.h"#include "app.h"int main() { return 0; }What to put in it
Headers that are large, included by many files, and rarely changed: system headers, the standard library, and external
libraries. Keep your own headers out of it, or only the most stable ones: each change to a header in pch.h
recompiles every file of the project.
pch.h — stable shared headers
#pragma once#include <string>#include <vector>#include <memory>app.cpp — frequently edited code
#include "pch.h"#include "changing_feature.h"// Changes here do not regenerate the PCH.Without changing every file
Set C/C++ > Advanced > Forced Include File to pch.h, and the compiler includes it at
the top of every file. This is convenient for an existing project, but the files then compile only with this setting.
Fix its errors
- C1010 unexpected end of file while looking for precompiled header: a file
does not include
pch.h. Include it first, or set that file to Not Using Precompiled Headers, which C files and third-party sources need. - C1083 cannot open precompiled header file (
.pch): no file of the project creates it. Check thatpch.cppis set to Create (/Yc) for this configuration and platform. - C2065 after adding a precompiled header: code placed before
#include "pch.h"is ignored. Move the include to the first line.
Sources and scope
Official references
These links document the compiler, build system, or debugger behavior discussed here. Use the version selector on Microsoft Learn for your toolset. Example blocks are focused excerpts; required headers and build settings are stated alongside them.
Using CppPilot in Visual Studio
CppPilot connects your own Codex or Claude account to Visual Studio. If you have neither, CppPilot Cloud provides AI access for build fixes, reviews, crash analysis, and project checks. AI chat requires your own Codex or Claude account.