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

前端 未结 4 1981
夕颜
夕颜 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:49

    You need to #include b.h in b.c. It is not just an interface for a.c, b.c needs to know the same definitions for its own code as well. Your reason for not including b.h in b.c is wrong. Each .c file is compiled separately from every other .c file. When the compiler is done with a.c, it starts over fresh with b.c. It does not matter that a.c included b.h, because b.c has no concept that a.c even exists. The purpose of a header guard is to prevent a .h file from being processed repeat times if it is included multiple times while compiling a given .c file. Without the guard, declarations would get compiled multiple times, causing errors about multiple declarations of existing symbols.

提交回复
热议问题