Include guard
In the C and C++ programming languages, an #include guard, sometimes called a macro guard, header guard or file guard, is a way to avoid the problem of double inclusion when dealing with the include directive. The C preprocessor processes inclusion directives like However, if an #include directive for a given file appears multiple times during compilation, the code will effectively be duplicated in that file. If the included file includes a definition, this can cause a compilation error due to the One Definition Rule, which says that definitions (such as the definition of a class) cannot be duplicated in a translation unit. #include guards prevent this by defining a preprocessor macro when a header is first included. In the event that header file is included a second time, the #include guard will prevent the actual code within that header from being compiled. An alternative to #include guards is #pragma once. This non-standard but commonly supported directive among C and C++ compilers has the same purpose as an #include guard, but has less code and does not require the definition of a variable. Double inclusionExampleThe following C code demonstrates a real problem that can arise if #include guards are missing: File "grandparent.h"struct foo {
int member;
};
File "parent.h"#include "grandparent.h"
File "child.c"#include "grandparent.h"
#include "parent.h"
Resultstruct foo {
int member;
};
struct foo {
int member;
};
Here, the file "child.c" has indirectly included two copies of the text in the header file "grandparent.h". This causes a compilation error, since the structure type Use of #include guardsExampleThe same code as the previous section is used with the addition of #include guards. The C preprocessor preprocesses the header files, including and further preprocessing them recursively. This will result in a working source file. File "grandparent.h"#ifndef GRANDPARENT_H
#define GRANDPARENT_H
struct foo {
int member;
};
#endif /* GRANDPARENT_H */
File "parent.h"#include "grandparent.h"
File "child.c"#include "grandparent.h"
#include "parent.h"
Intermediate step#ifndef GRANDPARENT_H // GRANDPARENT_H is not defined
#define GRANDPARENT_H
struct foo { // This definition is compiled
int member;
};
#endif /* GRANDPARENT_H */
#ifndef GRANDPARENT_H // GRANDPARENT_H is already defined
#define GRANDPARENT_H
struct foo { // This definition is not compiled
int member;
};
#endif /* GRANDPARENT_H */
Resultstruct foo {
int member;
};
Here, the first inclusion of "grandparent.h" has the macro DiscussionDifferent naming conventions for the guard macro may be used by different programmers. Other common forms of the above example include Of course, it is important to avoid duplicating the same include-guard macro name in different header files, as including the 1st will prevent the 2nd from being included, leading to the loss of any declarations, inline definitions, or other #includes in the 2nd header. DifficultiesFor #include guards to work properly, each guard must test and conditionally set a different preprocessor macro. Therefore, a project using #include guards must work out a coherent naming scheme for its include guards, and make sure its scheme doesn't conflict with that of any third-party headers it uses, or with the names of any globally visible macros. For this reason, most C and C++ implementations provide a non-standard Other languagesSome languages support specifying that the code should be included only once, in the including file, rather than in the included one (as with C/C++ include guards and
See alsoReferences
External links |