redefinition of typedef

前端 未结 7 924
北荒
北荒 2020-12-15 05:54

I am possibly doing this incorrectly and this is much a question about why it works in one compiler and not the other.

I have a large C application, and I am

相关标签:
7条回答
  • 2020-12-15 06:31

    The ubuntu compiler is being overly soft; you can't typedef the same thing twice. In the style you refer to, the ordering of the includes is important, and is usually mentioned as a comment in the header file, or in the documentation. In this case, you would have:

    //A.h
    typedef struct A A;
    struct A {
        double a;
        B* b;
    };
    
    // B.h
    typedef struct B B;
    struct B {
        int c;
    };
    
    // C.h
    void function_do_something(A*, B*);
    
    // C.c
    #include "B.h"
    #include "A.h"
    #include "C.h"
    
    void function_do_something(A* a, B* b){ ... }
    

    You might note that this will get messy in the case of cyclic dependancies.

    0 讨论(0)
提交回复
热议问题