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

前端 未结 5 1549
旧巷少年郎
旧巷少年郎 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条回答
  •  萌比男神i
    2020-12-11 22:53

    Header:

    extern const char    *Bittorrent;
    

    Main (before the main function / as gloabal variable):

    const char    *Bittorrent     =   "BitTorrent protocol";
    

    This way your header tells every file that there is variable named "Bittorrent", but only your main.c file has the part in it where it is really created.

提交回复
热议问题