CppPilot

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.

Applies toVisual Studio MSBuild C++ projects (.vcxproj)
ExamplesFocused excerpts; required context is stated alongside each example

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.

VideoFilmed in Visual Studio with a demo project. English narration, with subtitles in English, French, German, Italian and Spanish.
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.

How it worksCreate once, reuse across source files
  1. pch.hStable, shared headers
  2. pch.cpp /YcCreates the .pch file
  3. 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

  1. Create pch.h with the stable headers the project uses most, for example <windows.h>, <string>, <vector>, and third-party libraries.
  2. Create pch.cpp that contains only #include "pch.h".
  3. In the project properties, for all configurations, set C/C++ > Precompiled Headers > Precompiled Header to Use (/Yu) and Precompiled Header File to pch.h.
  4. In the properties of pch.cpp only, set Precompiled Header to Create (/Yc).
  5. Make #include "pch.h" the first line of every .cpp file.

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; }
Examplepch.h contains the stable shared headers. Configure pch.cpp to Create and the consuming sources to Use.

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.
ExampleKeep frequently changed project headers outside the shared PCH to reduce unnecessary regeneration.

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 that pch.cpp is 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.

Setup instructions · Visual Studio Marketplace