C中的共享全局变量 [英] shared global variables in C

查看:45
本文介绍了C中的共享全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建在 C 中共享的全局变量?如果我把它放在头文件中,那么链接器会抱怨变量已经定义.是在我的一个 C 文件中声明变量并手动将 extern 放在所有其他想要使用它的 C 文件顶部的唯一方法吗?这听起来不太理想.

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 declare the variable in one of my C files and to manually put in externs at the top of all the other C files that want to use it? That sounds not ideal.

推荐答案

在头文件中用extern写.并且在其中一个 c 文件的全局范围内声明它没有 extern.

In the header file write it with extern. And at the global scope of one of the c files declare it without extern.

这篇关于C中的共享全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆