预编译头文件PCH

(2 mins to read)

C++中的编译单元是一个cpp文件,它会展开其中所有include进来的头文件,然后进行编译。因此头文件会被大量地重复编译。

预编译头文件(PCH,precompiled header)就是预先把头文件编译好,以避免对头文件的重复编译。主要是用于基本不会改变的头文件。

clang pch

Precompiled headers are meant to improve overall compile times for projects, so the design of precompiled headers is entirely driven by performance concerns. The use case for precompiled headers is relatively simple: when there is a common set of headers that is included in nearly every source file in the project, we precompile that bundle of headers into a single precompiled header (PCH file). Then, when compiling the source files in the project, we load the PCH file first (as a prefix header), which acts as a stand-in for that bundle of headers.