How are circular #includes resolved?

后端 未结 4 1752
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 12:06

In c lets say we have 2 files

1.h

#include<2.h>

blah blah

and we have 2.h

#include<1.h>

code
         


        
4条回答
  •  隐瞒了意图╮
    2020-11-29 12:32

    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
    

提交回复
热议问题