/
The core idea of a defensive include

The core idea of a defensive include

We can solve the problem of including a header file twice by defining a unique preprocessor instruction constant that if it is encountered we do not process the body of the header file, i.e.:

#ifndef SOME_ARBITRARY_UNIQUE_CODE #define SOME_ARBITRARY_UNIQUE_CODE //.... body of header file goes here. #endif // end of the block which started at line 2

So how do you make a unique arbitrary code? There is more than one way to solve to solve this problem:

  • Have convention that maps the file name of the header file to a unique code

  • Or use something like a Globally Unique IDentifier - GUID.

Related content

Why do we need header files?
Why do we need header files?
More like this
Set up a dummy 'library' and unit test
Set up a dummy 'library' and unit test
More like this
Have a convention to make a unique identifier based on the file name for defensive includes
Have a convention to make a unique identifier based on the file name for defensive includes
More like this
What problem is created if a header file is included twice?
What problem is created if a header file is included twice?
More like this
Use randomly generated GUIDs for defensive includes
Use randomly generated GUIDs for defensive includes
More like this
C Preprocessor
C Preprocessor
More like this