include guards in C++

28 Jan, 2021
While programming in C++ we often use a class multiple times, and hence it requires to create a header file and just include it in the main program. Now, sometimes it happens that a certain header file directly or indirectly get included multiple times, then the class declared in the header file gets re-declared which gives an error. To understand the needs of include guards let us first understand one example:
Program 1: Creating an Animal class and save it as “Animal.h”. Below is the program for the same:
C++
// C++ program to create a header// file named as "Animal.h"#include <iostream>#include <string>using namespace std;  // Animal Classclass Animal {    string name, color, type;  public:    // Function to take input    void input()    {        name = "Dog";        color = "White";    }      // Function to display the member    // variables    void display()    {        cout << name << " is of "             << color << endl;    }};
Program 2: Creating a Dog class and save it as Dog.h. Remember to include “Animal.h” header file declared above:
C++
// C++ program to create header file// named as Dog.h  // Include the header file "Animal.h"#include "Animal.h"  // Dog Classclass Dog {    Animal d;  public:    // Take input to member variable    // using function call in another    // header file    void dog_input() { d.input(); }      // Function to display the member    // variable using function call    // in another header file    void dog_display() { d.display(); }};
Program 3: Create a main.cpp file and include above both header files. Below is the program for the same:
C++
// C++ program to illustrate the// include guards#include "Animal.h"#include "Dog.h"#include <iostream>using namespace std;  // Driver Codeint main(){    // Object of Dog class in    // "Dog.h" header file    Dog a;      // Member Function Call    a.dog_input();    a.dog_display();      return 0;}
Output: Now, when the above program “main.cpp” is executed then the following error occurs:
Explanation: When the “main.cpp” program is compiled using include Animal.h and defines Animal class, thereafter while including Dog.h, Animal.h get included and in main program there are two definitions of Animal Class that’s why this error is generated. Now lets resolve the issue using include guards.
In the C and C++ programming languages, an #include guard, sometimes called a macro guard, header guard, or file guard, is a particular construct used to avoid the problem of double inclusion when dealing with the include directive.
Solution:Include guards ensures that compiler will process this file only once, no matter how many times it is included. Include guards are just series of preprocessor directives that guarantees file will only be included once.Preprocessors used:
#ifndef: if not defined, determines if provided macros does not exists.
#define: Defines the macros.
#endif: Closes off #ifndef directive.
The block of statements between #ifndef and #endif will be executed only if the macro or the identifier with #ifndef is not defined.
Syntax: 
#ifndef ANIMAL(Any word you like but unique to program)
#define ANIMAL(same word as used earlier)

class Animal {
    // Code
};

#endif
So, the “Animal.h” header file should be declared as:
C++
// Checks if _ANIMALS IF DECLARED#ifndef _ANIMALS_  // Defines _ANIMALS_ if above// conditions fails#define _ANIMALS_  #include <iostream>#include <string>using namespace std;  // Animal Classclass Animal {    string name, color, type;  public:    // Function to take input to    // member variable    void input()    {        name = "Dog";        color = "White";    }      // Function to display the    // member variable    void display()    {        cout << name << " is of"             << color << endl;    }};#endif // _ANIMALS_
Output:
Explanation: When main.cpp runs, Animal.h is included and animal class is declared. Here the first line of Animal.h header while executed and as _ANIMALS_ is not defined code executes normally. When Dog.h header file gets included which in turn included Animal.h, this time _ANIMALS_ is defined in program, so first line #ifndef condition is true and entire code gets SKIPPED to last line i.e #endif. In simple words, if the preprocessor has that name defined then it skips entire file and goes to #endif, in other words it doesn’t process file.
CPP-Basics
Macro & Preprocessor
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.