Include Guard MacroEdit
To prevent inclusion of a header file multiple times.
Also Known AsEdit
Including the same header file in same compilation unit is a problem because it violates a basic rule of C++: One Definition Rule (ODR). A header may get included multiple times because of direct and indirect inclusion.
Solution and Sample CodeEdit
Include Guard macro idiom is an old idiom, which is also applicable in a C program. It used simple #define to prevent inclusion of a header file multiple times in a compilation unit. Following macros are put at the very beginning and at very end of a header file.
#ifndef MYHEADER_H_ // beginning #define MYHEADER_H_ ... #endif // MYHEADER_H_ // end
Some compilers support
as an efficient alternative to include guards. It does not require to open the header file more than once, unlike traditional include guard macro in some compilers. On many modern compilers like GCC4 or MSC++2008 #pragma once will not give better compile time performance as they recognize header guards.
Virtually all header files in the world!
- Inline Guard Macro
- Export Guard Macro
#pragma once in Wikipedia.