在头文件中的变量声明 [英] Variable declaration in a header file

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

问题描述

在情况下,我有可能在多个来源中使用的变量 - 它是一个很好的做法,声明它在头?还是更宣布它在 .C 文件和其他文件中使用的extern

In case I have a variable that may be used in several sources - is it a good practice to declare it in a header? or is it better to declare it in a .c file and use extern in other files?

推荐答案

您应该的声明的头文件中的变量:

You should declare the variable in a header file:

extern int x;

然后的确定的它的有一个的C文件:

int x;

在C中,定义和声明之间的差别在于,定义保留空间的变量,而声明仅仅介绍了变量到符号表(将导致链接器去寻找它,当涉及到链接时)。

In C, the difference between a definition and a declaration is that the definition reserves space for the variable, whereas the declaration merely introduces the variable into the symbol table (and will cause the linker to go looking for it when it comes to link time).

这篇关于在头文件中的变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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