Precompiled Header Files

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

Caution

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.

-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:
prompt>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

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.

-create_pch

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

Example 2 command line:
prompt>icpc -create_pch /pch/source32.pchi source.cpp

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

-use_pch filename

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

Example 3 command line:
prompt>icpc -use_pch /pch/source32.pchi source.cpp

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

-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, -create_pch filename, and -use_pch filename.

Example 4 command line:
prompt>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:

prompt>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.