The compiling of header files is made more efficient by using combinations of preprocessor directives. The best way to learn how to construct an efficient header file is to look at an example:
#if !defined(_INC_MEM ) #include <mem.h> // to get memcpy and NULL #endif #endif /* !_INC_IOSTREAM */
Before looking at the individual statements in the example, you need to know that passing one of the compiler builds a symbol table. One of the entry types in a symbol table is the manglednames of header files. Mangling is something that the compiler does to distinguish one symbol from another. The C compiler prepends an underscore to these symbols.
The easiest way to control the compiled visibility of a header file is to surround the code within the header file with a tri-statement combination in the form:
#ifndef _INC_MYHEADER #define _INC_MYHEADER /* begin _INC_MYHEADER visibility */ . . . #endif /* end of conditional _INC_MYHEADER visibility */
This is exactly what was done with the previous coded example where _INC_IOSTREAM was substituted for _INC_MYHEADER. The first time the compiler includes this header file, _INC_IOSTREAM is undefined. The code segment is included, making all of the nested statements visible. From this point forward, any additional #include <iostream.h> statements found in any of the other files used to create the executable bypass the nested code.