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

查看:115
本文介绍了用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 in one of the c files declare it without extern.

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

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