How are circular #includes resolved?

Typically you protect your include file with an ifndef/define that corresponds to the file name. This doesn’t prevent the file from being included again, but it does prevent the contents (inside the ifndef) from being used and triggering the recursive includes again.

 #ifndef HEADER_1_h
 #define HEADER_1_h

 #include "2.h"

 /// rest of 1.h

 #endif

 #ifndef HEADER_2_h
 #define HEADER_2_h

 #include "1.h"

 //  rest of 2.h

 #endif

Leave a Comment