What is pch.h

When I create a new C/C++ project in VisualStudio, an header file named “pch.h” is generated.

GOAL

To understand what is pch.h and how to use it.

Environment

Windows 10
VisualStudio 2017

What is pch.h

pch.h is a precompiled header that is an header file where any stable header files such as Standard Library headers are included. You can add header files you’d like to pre compile. The precompiled header is compiled only when it, or any files it includes, are modified.

Reference: Precompiled Header Files in Microsoft documentation

The following is initial pch.h

#ifndef PCH_H
#define PCH_H

// TODO: add headers that you want to pre-compile here
#endif //PCH_H

The advantage of pch.h

Using pch.h reduce compilation time, especially when applied to large header files, header files that include many other header files.

How to use pch.h

Add header files you’d like to use in the pch.h as below.

#ifndef PCH_H
#define PCH_H

#include <stdio.h>
#include <math.h>
#include "myheader.h"

#endif //PCH_H