Why does global variables in a header file cause link error?

前端 未结 5 1545
旧巷少年郎
旧巷少年郎 2020-12-11 22:03

I always get the following error, even if I have put include guard in header file.

duplicate symbol _Bittorrent in:
    /Users/tracking/Library/Developer/Xco         


        
5条回答
  •  春和景丽
    2020-12-11 22:53

    By initializing the variable, you are defining it in every place that the file is included. Assuming that these modules are linked together, you now have multiple definitions, which is an error.

    You can declare the variable in your header with an extern tag and then initialize it in one of your .c modules.

    In your .h file:

    extern int i;
    

    In one of your .c files:

    int i = 1;
    

    However,

提交回复
热议问题