Watcom C/C++ supports the use of precompiled headers to decrease the time required to compile several source files that include the same header file. This chapter includes the following sections:
Using precompiled headers reduces compilation time when:
Because the compiler only uses the first include file to create a precompiled header, you may want to create a master or global header file that includes all the other header files that you wish to have precompiled. Then all source files should include this master header file as the first #include in the source file. Even if you don't use a master header file, you can benefit from using precompiled headers for Windows programs by using #include <windows.h> as the first include file, or by using #include <afxwin.h> as the first include file for MFC applications.
The first compilation - the one that creates the precompiled header file - takes a bit longer than subsequent compilations. Subsequent compilations can proceed more quickly by including the precompiled header.
You can precompile C and C++ programs. In C++ programming, it's common practice to separate class interface information into header files that can later be included in programs that use the class. By precompiling these headers, you can reduce the time a program takes to compile.
Precompiled code is stored in a file called a precompiled header when you use the precompiled header option (-fh or fhq) on the command line:
The default name of the precompiled header file is one of wcc.pch, wcc386.pch, wpp.pch, or wpp386.pch (depending on the compiler used). You can also control the name of the precompiled header that's created or used with the -fh=filename or -fhq=filename (``specify precompiled header filename'') options. For example,
The -fh option instructs the compiler to use a precompiled header file with a default name of wcc.pch, wcc386.pch, wpp.pch, or wpp386.pch (depending on the compiler used) if it exists, or to create one if it does not. The file is created in the current directory. You can use the -fh=filename option to change the default name (and placement) of the precompiled header. Add the letter q (for ``quiet'') to the option name to prevent the compiler from displaying precompiled header activity information.
The following command line uses the -fh option to create a precompiled header:
wpp -fh myprog.cpp wpp386 -fh myprog.cpp
The following command line creates a precompiled header named myprog.pch, and places it in the /projpch directory:
wpp -fh=/projpch/myprog.pch myprog.cpp wpp386 -fh=/projpch/myprog.pch myprog.cpp
The precompiled header is created and/or used when the compiler encounters the first #include directive that occurs in the source file. In a subsequent compilation, the compiler performs a consistency check to see if it can use an existing precompiled header. If the consistency check fails then the compiler discards the existing precompiled header and builds a new one.
The -fhq form of the precompiled header option prevents the compiler from issuing warning or informational messages about precompiled header files. For example, if you change a header file, the compiler will tell you that it changed, and that it must regenerate the precompiled header file. If you specify -fhq then the compiler just generates the new precompiled header file without displaying a message.
If a precompiled header file exists (either the default file or one specified by the -fh=filename option), it's compared to the current compilation for consistency:
If these conditions aren't met, a new precompiled header file is created, and the new file overwrites the old.