Linking .h files with .c with #ifdef header guards

前端 未结 4 1988
夕颜
夕颜 2020-12-09 05:09

im having trouble linking .h and .c files, i\'ve also read some threads regarding this problem and all of them is a bit vague and still i can\'t fully grasp the concept of i

4条回答
  •  佛祖请我去吃肉
    2020-12-09 05:44

    You need to include b.h in all files that uses the structures that are defined in b.h. So you need to put a #include in both files. To avoid that b.h is loaded several times, you need the directives #ifdef. In your case:

    b.h

    #ifndef B_H
    #define B_H
    
    typedef struct{
        int x, y;
    }myStruct;
    
    void funct1(myStruct);
    void funct2(myStruct);
    
    #endif
    

    and b.c:

    #include "b.h"
    
    void funct1(myStruct x)
    {
        //do something
    }
    
    void funct2(myStruct y)
    {
         //do something
    } 
    

提交回复
热议问题