Include guard
≈ 6 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In the C and C++ programming languages, an 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 #include "Foo.h" to textually include and transcludes the code of that file into a copy of the main file often called the translation unit. 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. Modules, introduced in C++20, eliminate the necessity of #include guards, due to not being handled by the preprocessor.…
In the C and C++ programming languages, an 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 #include "Foo.h" to textually include and transcludes the code of that file into a copy of the main file often called the translation unit. 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. Modules, introduced in C++20, eliminate the necessity of #include guards, due to not being handled by the preprocessor. Modules can only be imported at most one time into a translation unit. ==Double inclusion== struct Foo { int member; }; include "Grandparent.h" include "Grandparent.h" include "Parent.h" // From "Grandparent.h" struct Foo { int member; }; // From "Parent.h" struct Foo { int member; }; Here, the file has indirectly included two copies of the text in the header file . This causes a compilation error, since the structure type Foo will thus be defined twice. In C++, this would be called a violation of the one definition rule. ==Use of #include guards== ====File "Grandparent.h"==== ifndef GRANDPARENT_H define GRANDPARENT_H struct Foo { int member; }; endif // GRANDPARENT_H include "Grandparent.h" include "Grandparent.h" include "Parent.h" // Contents from "Grandparent.h" ifndef GRANDPARENT_H // GRANDPARENT_H was not defined define GRANDPARENT_H // This definition was pasted struct Foo { int member; }; endif // GRANDPARENT_H // Contents from "Parent.h" ifndef GRANDPARENT_H // GRANDPARENT_H was already defined define GRANDPARENT_H // This definition was not pasted struct Foo { int member; }; endif // GRANDPARENT_H struct Foo { int member; }; Here, the first inclusion of " " has the macro GRANDPARENT_H defined. When " " includes " " at the second time (while including " "), as the #ifndef test returns false, the preprocessor skips down to the #endif , thus avoiding the second definition of struct Foo . The program compiles correctly. ==Difficulties== For #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 #pragma once directive. This directive, inserted at the top of a header file, will ensure that the file is included only once. The Objective-C language (which is a superset of C) has an #import directive, which works exactly like #include , except that it includes each file only once, thus obviating the need for #include guards. ==Other languages== Some 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 #pragma once ): PL/I uses the %INCLUDE statement as the equivalent to C's #include directive. IBM Enterprise PL/I also supports the %XINCLUDE statement which will "incorporate external text into the source program if it has not previously been included." (It also offers an XPROCEDURE statement, similar to a PROCEDURE statement, which will ignore the second and subsequent occurrences of an XPROCEDURE with the same name.) Objective-C's #import directive (see above) PHP's include_once ==See also== #pragma once C preprocessor Circular dependency One Definition Rule PL/I preprocessor ==References== ==External links== Include guard optimisation Redundant Include Guards
Text derived from https://en.wikipedia.org/w/index.php?oldid=1362296348 under CC BY-SA 4.0; modifications have been made.