Include guard
Encyclopedia : I : IN : INC : Include guard
If you disagree with this, please explain why on . If this page obviously does not meet the criteria for speedy deletion, or you intend to fix it, please remove this notice, but do not remove this notice from a page that you have created yourself.
''[AdministratorsAdministrators], remember to check , [ the page history] ([ last edit]) and any revisions of CSD before moving . The substitution or omission of a # sign is due to [Naming conventions #Characters not allowed at all in page titlestechnical restrictions].}}
In the C programming language, an #include guard is a particular construct used to avoid the problem of double inclusion when dealing with the
struct foo ;
#endif
For this reason, many C and C++ implementations provide the non-standard directive
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.#include directive, as demonstrated in the following sample C code:Double inclusion
File \"grandfather.h\"
struct foo ;
File \"father.h\"
#include "grandfather.h"
File \"child.c\"
#include "grandfather.h"
#include "father.h"
Here, the file "child.c" has indirectly included two copies of the text in the header file "grandfather.h". This causes a compilation error, since the structure foo is apparently defined twice!Use of #include guards
File \"grandfather.h\"
#ifndef H_GRANDFATHER
#define H_GRANDFATHER
File \"father.h\"
#include "grandfather.h"
File \"child.c\"
#include "grandfather.h"
#include "father.h"
Here, the first inclusion of "grandfather.h" causes the macro H_GRANDFATHER to be defined. Then, when "father.h" includes "grandfather.h" the second time, the #ifndef test fails, and the compiler skips down to the #endif, thus avoiding the second definition of struct foo. The program compiles correctly.Difficulties
In order 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.#pragma once. This directive, inserted at the top of a header file, will ensure that the file is only included once. See also
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
