Using a Precompiled Header File

Although a precompiled file is not a text file, you use it like you would a regular header file. To include a precompiled header file in a source code file, use the #include directive.

NOTE Unlike regular header files in text format, a source code file may include only one precompiled file.

TIP Instead of explicitly including a precompiled file in each source code file with the #include directive, put the precompiled file name in the Prefix File field of the C/C++ Language settings panel. If the Prefix File field already specifies a file name, include the precompiled file in the prefix file with the #include directive.

Listing 5.1 and Listing 5.2 show an example.

Listing 5.1 Header File that Creates a Precompiled Header File for C

// sock_header.pch
// When compiled or precompiled, this file will generate a
// precompiled file named "sock_precomp.mch"

#pragma precompile_target "sock_precomp.mch"

#define SOCK_VERSION "SockSorter 2.0"
#include "sock_std.h"
#include "sock_string.h"
#include "sock_sorter.h"

Listing 5.2 Using a Precompiled File

// sock_main.c
// Instead of including all the files included in
// sock_header.pch, we use sock_precomp.h instead.
//
// A precompiled file must be included before anything else.

#include "sock_precomp.mch"

int main(void)
{
// ...
return 0;
}