4/17/08

How to make sure that a file is not included more than once while compiling code ?

A header file should begin and end with #include guards to guarantee that it's not #included more than once during the same compilation session. A typical #include guard looks as follows:
// from stdlib.h
#ifndef File_Name_InCluded// included for the first time?
#define File_Name_InCluded

// declarations, typedef's etc.

#endif // close #include guard at the end of the file

Inside the header file, you may insert function prototypes, structs declarations, #define macros, enum types, typedef's, class declarations, extern declarations of global objects, template classes and functions, constants, certain #pragma directives that control the compilation process and similar macro directives. Note, however, that a header file should never have definitions (except for macros and templates). In other words, you shouldn't implement functions and class member functions inside a header file (except for inline functions), nor should you create variables, arrays, and object or global variables in a header file.

No comments:

ITUCU