Are include guards considered defined after the #define directive or after the #endif directive

62 views Asked by At

I have a circular inclusion considering these files:

fileA.hh

#ifndef FILEA_H
#define FILEA_H

#include "fileB.hh"

// Use a function in fileB

#endif

fileB.hh

#ifndef FILEB_H
#define FILEB_H

#include "fileA.hh"

// use an enum in fileA

#endif

I managed to resolve the issue by creating a fileA.cc in which I would include both fileA.hh and fileB.hh, and I removed the inclusion of fileB.hh in fileA.hh. All good.

But that got me thinking, why is my example resulting in a circular inclusion ? If I copy paste fileB.hh in fileA.hh, I should have something like this:

#ifndef FILEA_H
#define FILEA_H

#ifndef FILEB_H
#define FILEB_H

#ifndef FILEA_H
#define FILEA_H

// Use a function in fileB

#endif

// use an enum in fileA

#endif

// Use a function in fileB

#endif

When I encounter the second #ifndef FILEA_H shouldn't it be already defined just above ? Or does that mean that FILEA_H is really defined only after the #endif and not only after the #define ? How does it work exactly ?

0

There are 0 answers