Firstly, #include
is a preprocessor directive that performs full textual substitution of one text file into another text file. Two header files trying to #include
each other form an infinite loop of nested textual substitutions. I think it should be obvious that an infinite loop of textual substitutions will not "work", simply because it is infinite.
Secondly, using #ifndef
include guards in the header file will simply break the infinite loop at some point. I.e. the circular inclusion will turn into sequential inclusion with one file included first and another file included second. But sequential inclusion (in any order) will not help to resolve any circular declaration dependencies present in your header files.
For this reason circular inclusion of header files never makes any sense (aside form very special contexts, like preprocessor tricks), regardless of whether you use include guards or not. Circular inclusion never achieves anything. You have to design your header files so that they don't even attempt to rely on circular inclusion. I.e. you have to stratify your declarations and your header files into lower-level and higher-level ones, and always include lower into higher (but not the other way around), and resolve any circular declaration dependencies by using forward-declarations in lower-level headers.
Sometimes header files call for circular inclusion simply because they are poorly designed. For example, even when there are no circular dependencies between the declarations, these declarations might be incorrectly distributed between header files, leading to a perceived need to include headers into each other circularly. In such cases it is always a better idea to refactor your headers in order to eliminate any circular declaration dependencies. E.g. redistribute declarations between the headers, extract portions of two mutually dependent headers into a third lower-level header etc. And only when such refactoring is not possible, i.e. you have a genuine circular declaration dependency, use forward-declarations as a last resort.