C-sharpinclude circular reference problem, header file circular reference

with regard to the circular reference problem in C, I hope to learn how to avoid such errors in header file circular reference.

general logical description

main.c: is the program entry,
a.h include .h is the header file;
where
a.h is include e.h;
c.h include b.h;
b.h include a.h

run the program to report an error,

related codes

main.c
-sharpinclude <stdio.h>
-sharpinclude "a.h"

int main(int argc, const char * argv[]) {
    printf("Hello ! \n");
    return 0;
}
a.h
-sharpifndef a_h
-sharpdefine a_h

-sharpinclude "c.h"

struct sem
{
    struct eve *evet;
};


-sharpendif /* a_h */
b.h
-sharpifndef b_h
-sharpdefine b_h

-sharpinclude "a.h"

struct pan
{
    struct sem semt;
};

struct dev
{
    int x;
};

-sharpendif /* b_h */
c.h
-sharpifndef c_h
-sharpdefine c_h

-sharpinclude "b.h"

struct eve
{
    struct dev *devt;
};


-sharpendif /* c_h */

looks forward to giving reasonable examples and solutions to the circular references of header files. The reason why the problems are sent out is to help more people who encounter similar problems, and hope that the answers are relatively comprehensive.

C cPP
Jun.21,2022

Loop references have to be circumvented by design.
for example, in your case, the root cause is the dependency of pan- > sam- > eve- > dev, while pan and dev are declared in the same header file.
the solution is simply to separate the declaration of dev from that of pan.

A more common practice is pre-declaration + pointer.

after all, cPP needs to implement

.
class A
{
    B b;
};
class B
{
    A a;
};

is impossible.


example 1

description: header1.h contains header2.h; header2.h, contains header1.h;

/** circular dependency --  */
-sharpinclude <stdio.h>
-sharpinclude "header1.h"

int main(void) {
    printf("this is my function!");
    return 0;
}

header1.h

-sharpifndef Header1_H_
-sharpdefine Header1_H_

-sharpinclude "header2.h"

/*
=========header2.h=======

========header1.h======
-sharpinclude <header1.h>
 header1.h 
-sharpifndef Header1_H_
-sharpdefine Header1_H_
 header1_H_ ;;
// Other content
-sharpendif  
========header1.h=====

struct A2{
    int value;
};

=========header2.h=========

*/

/**
 *    A2 ;  header2.h ; eg: struct A2 *p;
 *   void * p ;
*/


/**
 * A2, 
*/
struct A1 {
    int value;
    struct A2 p;
};

-sharpendif 

header2.h

-sharpifndef Header2_H_
-sharpdefine Header2_H_

-sharpinclude "header1.h"

struct A2{
    int value;
};

-sharpendif

this is solved by-sharpifndef XeroH_-sharpdefine XeroHH- sharpendif;

[original reference] https://stackoverflow.com/que...

[Headers and Includes: Why and How] http://www.cplusplus.com/foru...

[Avoiding Circular Dependencies of header files
] https://stackoverflow.com/que...

Menu