shared global variables in C

前端 未结 6 657
星月不相逢
星月不相逢 2020-11-28 20:27

How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to de

6条回答
  •  盖世英雄少女心
    2020-11-28 20:52

    If you're sharing code between C and C++, remember to add the following to the shared.hfile:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    extern int my_global;
    /* other extern declarations ... */
    
    #ifdef __cplusplus
    }
    #endif
    

提交回复
热议问题