Include guard
The include guard or include Guardian is a programming technique in C and C ++ to solve the problem of multiple integration.
This problem occurs when the same header file is included several times within a translation unit . This is usually done unintentionally, e.g. B. if several modules use the same library .
Example of a multiple integration
The following two header files A.h
and are given B.h
. a. define one class each :
// A.h
const int M = 123;
class A
{ /* ... */ };
// B.h
#include "A.h"
class B : public A
{ /* ... */ };
In the main program, both class A and B should now be used:
1 // program.cpp
2 #include "A.h"
3 #include "B.h"
4
5 int main() { /* ... */ }
The program cannot be compiled without errors because class A is defined twice: The first definition is made when the header file is included A.h
in line 2. The second definition is made because the header file B.h
, which is included in line 3, itself A.h
involves. The class (and possibly other variables) from this file have already been defined beforehand, which leads to a compilation error.
Solution with preprocessor macros (#ifndef)
The #ifndef wrapper is the traditional and C-compliant approach. The problem of multiple integration is solved by defining a unique preprocessor macro when the header file is first included. If the macro was already defined, the following definitions of the header are skipped:
// A.h
#ifndef A_H
#define A_H
class A
{ /* ... */ };
#endif /* A_H */
The above preprocessor commands cause the first time incorporation of Ah (from program.cpp ) the macro A_H is not yet defined and the preprocessor passes through the definitions. With the second integration (from Bh ) the macro is already defined and the preprocessor skips the block #ifndef… #endif .
Since macros exist in the global namespace, problems can arise here if an attempt is made to use the same name elsewhere (so-called namespace pollution ). This can be prevented by specifying naming conventions, but it does not solve the problem. Identifiers with a leading underscore (_) are reserved for compiler-specific definitions and definitions in the standard libraries. A leading underscore may therefore not be used in the application code according to the current C and C ++ standards.
The CPP , the preprocessor of the GCC , recognizes such constructs automatically, notes the corresponding files and skips them when they are integrated again.
Solution via language extensions (#pragma once)
Most of the C ++ compilers in use today support the language extension #pragma once
. This also ensures that a (header) file is only integrated once, but starts at a higher level, directly at the preprocessor, and does not introduce macros into the global namespace.
To use it, it is sufficient to #pragma once
insert the preprocessor directive within the header file :
// A.h
#pragma once
class A
{ /* ... */ };
This language extension is supported by the following C ++ implementations:
- GCC ,
- Clang ,
- Microsoft Visual C ++ ,
- Intel C ++ compiler ,
- Comeau C / C ++ ,
- C ++ Builder XE3,
- Digital Mars C ++ .
Individual evidence
- ↑ Eric Fleegal's WebLog ( page no longer available , search in web archives ) Info: The link was automatically marked as defective. Please check the link according to the instructions and then remove this notice. on MSDN Blogs; Retrieved August 19, 2011.
- ↑ C ++ standard (ISO / IEC 14882); C ++ working draft N3242 (PDF), section 17.6.4.3.2
- ↑ C standard (ISO / IEC 9899); Committee Draft N1124 (PDF), Sections 6.10.8 and 7.1.3 / 1.
- ↑ Once-Only Headers in the GNU CPP Online Manual. Retrieved August 19, 2011.
- ↑ GCC 3.4 Release Series - Changes, New Features, and Fixes . Retrieved May 16, 2014.
- ↑ clang: Pragma.cpp Source File ( Memento of the original from April 4, 2014 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. . Retrieved May 16, 2014.
- ↑ MS Developer Network - once (C / C ++) . Retrieved May 16, 2014.
- ↑ Comeau C ++ 4.0 ( Memento of the original from December 11, 2013 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. - Pre-Release - User Documentation: Pragmas ( Memento of the original dated December 11, 2013 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. . Retrieved May 16, 2014.
- ↑ #pragma once - RAD Studio XE3 . Retrieved May 16, 2014.
- ↑ Pragmas #pragma once . Retrieved May 16, 2014.