answersLogoWhite

0

C++ programs consist of one or more translation units that must be compiled and linked to create an executable. Each translation unit is compiled separately to produce an object file. When all translation units are compiled, the object files are linked.

Translation units must be preprocessed before they can be compiled. That is, all macros must be expanded (replacing macro tokens with their expanded definitions) and all required include files are pulled in. The end result is an intermediate file which is the actual file processed by the compiler. During preprocessing of a translation unit, the include files for that unit are also preprocessed and expanded as necessary.

Not all translation units need to be compiled in every compilation. If a modification to the program has no effect upon a translation unit, the object file generated by a prior compilation can be used instead. This helps speed up the compilation process.

Headers that require little or no maintenance (such as standard library headers) can also be precompiled to help speed up the process even further.

Header files are often included in several translation units but can only be included once per compilation. To avoid unnecessary inclusions, headers typically use special macros known as inclusion guards of the following form:

// file: my_header.h

#ifndef _MY_HEADER_H_

#define _MY_HEADER_H_

// content goes here

#endif _MY_HEADER_H_

Inclusion guards typically use the file name as the basis for the macro definition and are deliberately ugly to avoid name clashes with other macros. The first time the header is processed, the macro will be undefined thus the content can be pulled into the intermediate file. All subsequent inclusions will see that the macro is already defined and will ignore the content.

User Avatar

Wiki User

10y ago

What else can I help you with?