使用“extern”;定义变量时的关键字
看到这个答案后 我有这个疑问。 在我的项目中, 我看到了一些声明和定义的外部变量,如下所示:
file1.h
extern int a;
file1.c
extern int a=10;
但在链接中我提到它说在 c 文件中它应该定义如下:
int a = 10;
在定义期间添加 extern
关键字是否也有任何目的/含义。 或者顺便说一下这很重要吗?
After seeing this answer
I have this doubt.
In my project,
I have seen some extern variables declared and defined like below:
file1.h
extern int a;
file1.c
extern int a=10;
But in the link I mentioned it says that in the c file it should be defined like:
int a = 10;
Does adding extern
key word during the definition too has any purpose/meaning.
Or does it matter by the way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不会改变含义。
extern
仅当您声明变量时才有意义。使用extern
定义变量是相同的,因为所有未标记为static
的全局变量默认都是链接器可见的符号。请注意,如果您不想初始化变量,即没有
= 10
部分,编译器将假定extern int a
始终为 声明而不是定义。同样的道理,全局的int a
始终是一个定义,而不仅仅是一个声明。It does not change the meaning.
extern
only makes sense when you declare a variable. Defining a variable withextern
is the same because all global variables that are not markedstatic
are symbols visible to the linker by default.Note that if you didn't want to initialise the variable, that is, not having the part
= 10
, the compiler will assume thatextern int a
is always a declaration and not a definition. In the same sense, havingint a
globally is always a definition and not just a declaration.这取决于。在这种情况下,这没有什么区别,但可以。
这里有两个问题:定义与声明,以及链接。
C++ 并不以正交方式处理它们。在 C++ 中,
变量或引用的声明是定义当且仅当
extern
关键字和初始化都不存在。 (笔记规则对于函数、类型和模板是不同的。)所以:
规则规定你必须有一个定义,所以你放了一个
定义在源文件中,声明在标头中。
关于链接,声明为变量或引用的符号
如果在名称空间范围内声明,则具有外部链接,但不是
声明为
static
,并且不是const
(在 C++11 中也不是constexpr
)或已声明
extern
。事实上const
可以给出变量内部链接有时意味着
extern
是必要:
请注意,
extern
不必位于同一个声明中:尽管如此,我偶尔也需要
extern
;通常是因为我想要使用局部常量实例化模板。 (这只是一个
如果模板参数采用指针或引用,则会出现问题。你可以
使用
int
参数和int const a = 实例化模板
,因为模板是用值 42 实例化的,而不是42;
变量
a
。)It depends. In this case, it makes no difference, but it can.
There are two issues here: definition vs. just declaration, and linkage.
And C++ doesn't handle them in an orthogonal manner. In C++, the
declaration of a variable or a reference is a definition if and only if
neither the
extern
keyword nor an initialization are present. (Notethat the rules are different for functions, types and templates.) So:
The rules say you must have exactly one definition, so you put a
definition in a source file, and the declaration in a header.
With regards to linkage, a symbol declared as a variable or a reference
has external linkage if it is declared at namespace scope, is not
declared
static
, and is either notconst
(norconstexpr
in C++11)or has been declared
extern
. The fact thatconst
can give avariable internal linkage occasionally means that the
extern
isnecessary:
Note that the
extern
doesn't have to be on the same declaration:Still, I've occasionally needed the
extern
; typically because I wantto use a local constant to instantiate a template. (This is only an
issue if the template parameter takes a pointer or a reference. You can
instantiate a template with an
int
parameter with anint const a =
, because the template is instantiated with the value 42, and not42;
the variable
a
.)