Using Precompiled Header Files

The Intel® C++ Compiler supports precompiled header (PCH) files to significantly reduce compile times using the following options:

Caution iconCaution

Depending on how you organize the header files listed in your sources, these options may increase compile times. See Organizing Source Files to learn how to optimize compile times using the PCH options.

Using -pch

The -pch option directs the compiler to use appropriate PCH files. If none are available, they are created as sourcefile.pchi. This option supports multiple source files, such as the ones shown in Example 1:

Example 1 command line:
icpc -pch source1.cpp source2.cpp

Example 1 output when .pchi files do not exist:

"source1.cpp": creating precompiled header file "source1.pchi"
"source2.cpp": creating precompiled header file "source2.pchi"

Example 1 output when .pchi files do exist:

"source1.cpp": using precompiled header file "source1.pchi"
"source2.cpp": using precompiled header file "source2.pchi"

Note iconNote

The -pch option will use PCH files created from other sources if the headers files are the same. For example, if you compile source1.cpp using -pch, then source1.pchi is created. If you then compile source2.cpp using -pch, the compiler will use source1.pchi if it detects the same headers.

Using -pch-create

Use the -pch-create filename option if you want the compiler to create a PCH file called filename. Note the following regarding this option:

Example 2 command line:

icpc -pch-create /pch/source32.pchi source.cpp

Example 2 output:

"source.cpp": creating precompiled header file "/pch/source32.pchi"

Using -pch-use filename

This option directs the compiler to use the PCH file specified by filename. It cannot be used in the same compilation as -pch-create filename. The -pch-use filename option supports full path names and supports multiple source files when all source files use the same .pchi file.

Example 3 command line:

icpc -pch-use /pch/source32.pchi source.cpp

Example 3 output:

"source.cpp": using precompiled header file /pch/source32.pchi

Using -pch-dir dirname

Use the -pch-dir dirname option to specify the path (dirname) to the PCH file. You can use this option with -pch, -pch-create filename, and -pch-use filename.

Example 4 command line:

icpc -pch -pch-dir /pch/source32.cpp

Example 4 output:

"source32.cpp": creating precompiled header file /pch/source32.pchi

Organizing Source Files

If many of your source files include a common set of header files, place the common headers first, followed by the #pragma hdrstop directive. This pragma instructs the compiler to stop generating PCH files. For example, if source1.cpp, source2.cpp, and source3.cpp all include common.h, then place #pragma hdrstop after common.h to optimize compile times.

#include "common.h"

#pragma hdrstop

#include "noncommon.h"

When you compile using the -pch option:

icpc -pch source1.cpp source2.cpp source3.cpp

the compiler will generate one PCH file for all three source files:

"source1.cpp": creating precompiled header file "source1.pchi"
"source2.cpp": using precompiled header file "source1.pchi"
"source3.cpp": using precompiled header file "source1.pchi"

If you don't use #pragma hdrstop, a different PCH file is created for each source file if different headers follow common.h, and the subsequent compile times will be longer. #pragma hdrstop has no effect on compilations that do not use these PCH options.

See Also