You can reduce compilation time and object file size by precompiling common include (header) files. Then when you compile your application or library, you specify the precompiled header file on the command line.
Note that just one precompiled header file is allowed per compilation.
To create and use a precompiled header file, follow these steps:
.C) file that includes all the header files you
want to precompile (headers.C in the following example).
precomp)
from headers.C by using the +hdr_create option.
Use the -c option to suppress creation of the executable file.
Each time you use the +hdr_create option to create a
precompiled header file, by default, a corresponding .o file is
also created. Information resulting from the compilation of declarations
is put into this .o file. The .o file may contain information related to
debugging, virtual function tables, and inline function bodies.
This saves space and time by eliminating duplication in future
+hdr_use compiles.
aCC headers.C -c +hdr_create precomp
precomp, use the
+hdr_use option.
This is known as a load compile.
aCC main.C +hdr_use precomp
Use the +hdr_v option for verbose information when precompiling a header or when compiling a precompiled header file.
To see what goes into the precompiled header file:
aCC headers.C -c +hdr_create precomp +hdr_v
To see what is being brought into the compiler during a load compile:
aCC main.C +hdr_use precomp +hdr_v
The files in the example below are written so that you can compile them either with or without precompiled headers, as described in Writing Headers that can be Either Compiled or Precompiled. Thus you could issue the following command to compile the files without precompiling:
aCC main.C
In the following example, header file a.h is included in precomp.C and precomp.C is included in main.C.
// a.h#ifndef A_H #define A_H extern "C" int printf(char *, ...); class foo { private: int x; public: foo() { printf("constructor for foo\n");x++; } }; #endif // A_H
// precomp.C
#ifndef PRECOMP
#define PRECOMP
#include "a.h"
class bar : foo {
private:
int y;
public:
bar() { printf("constructor for bar\n");y++; }
};
#endif // PRECOMP
// main.C
#include "precomp.C" // Use this include statement
// ONLY if you want
// headers that can be
// either precompiled or compiled.
void main()
{
bar b;
}
If you want to be able to either compile a header file directly or to precompile and than compile it (a load compile), the file must have the following characteristics.
.C) file that includes all of the other header files you intend to
precompile.
The +hdr_use option has the side effect of defining a compilation flag (e.g. A_H and PRECOMP), so the conditional directives prevent redefinitions of the contents of a.h and precomp.C respectively.
For more information about preprocessor directives, refer to Preprocessing in HP aC++
Be certain you remake a precompiled header file if anyone could have changed any of the header files it represents. Otherwise these header changes will not be part of the load compile.
It is also recommended that, from time to time, you recompile everything (header and .C files) without the +hdr_use option. This ensures that each .C file contains exactly the correct #include directives.
For example, suppose you have source files prog1.C and prog2.C that each require a #include prog.h directive. If the #include directive is missing from prog2.C, an error message would be generated if you recompile that file without the +hdr_use option. With the +hdr_use option, however, no error is generated since prog.h is in the precompiled header file because of the prog1.C #include directive.
When project files are under active development, it is sometimes better to divide your header files and precompiled header files into groups, depending on how often they are being modified. For example,
StableHeaders.C that includes only headers
that are changed infrequently.
VolatileHeaders.C that includes only headers
that are frequently changed. (If you use include guards, this file can
include headers from the set in StableHeaders.C.)
aCC StableHeaders.C -c +hdr_create Stable aCC VolatileHeaders.C +hdr_use Stable -c +hdr_create Total
aCC Prog.C +hdr_use Total