Include guard

1

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 to include "foo.h" 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, often times constructs are then defined twice, and the resulting translation unit is invalid. #include guards prevent this by defining a preprocessor macro when a header is first included, and detecting its presence to ignore the file's contents next time it is included. The addition of #include guards to a header file is one way to make that file idempotent and the program safer. Another construct to combat double inclusion is #pragma once, which is non-standard but nearly universally supported directive among C and C++ compilers.

Double inclusion

Example

The following C code demonstrates a real problem that can arise if #include guards are missing:

File "grandparent.h"

File "parent.h"

File "child.c"

Result

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 will thus be defined twice. In C++, this would be called a violation of the one definition rule.

Use of #include guards

Example

The 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"

File "parent.h"

File "child.c"

Result

Here, the first inclusion of "grandparent.h" has the macro defined. When "child.c" includes "grandparent.h" at the second time (while including "parent.h"), as the test returns false, the preprocessor skips down to the , thus avoiding the second definition of. The program compiles correctly.

Discussion

Different naming conventions for the guard macro may be used by different programmers. Other common forms of the above example include, (with the appropriate time information substituted), and names generated from a UUID. (However, names starting with one underscore and a capital letter (C and C++) or any name containing double underscore (C++ only), such as and , are reserved to the language implementation and should not be used by the user. ) 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.

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 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 directive, which works exactly like , 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 ):

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article