shared global variables in C

前端 未结 6 656
星月不相逢
星月不相逢 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 20:50

    You put the declaration in a header file, e.g.

     extern int my_global;
    

    In one of your .c files you define it at global scope.

    int my_global;
    

    Every .c file that wants access to my_global includes the header file with the extern in.

提交回复
热议问题