Should variable definition be in header files?

后端 未结 4 1386
一整个雨季
一整个雨季 2020-12-01 10:30

My very basic knowledge of C and compilation process has gone rusty lately. I was trying to figure out answer to the following question but I could not con

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 11:36

    You have two .c files. They get compiled separately. Each one includes your header file. Once. Each one gets a definition. They conflict at link time.

    The conventional solution is:

    #ifdef DEFINE_SOMETHING
    int something = 0;
    #endif
    

    Then you #define DEFINE_SOMETHING in only one .c file.

提交回复
热议问题