使用“extern”;定义变量时的关键字

发布于 2024-12-07 11:47:48 字数 465 浏览 0 评论 0原文

看到这个答案后 我有这个疑问。 在我的项目中, 我看到了一些声明和定义的外部变量,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

檐上三寸雪 2024-12-14 11:47:48

它不会改变含义。 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 with extern is the same because all global variables that are not marked static 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 that extern int a is always a declaration and not a definition. In the same sense, having int a globally is always a definition and not just a declaration.

清醇 2024-12-14 11:47:48

这取决于。在这种情况下,这没有什么区别,但可以。

这里有两个问题:定义与声明,以及链接。
C++ 并不以正交方式处理它们。在 C++ 中,
变量或引用的声明是定义当且仅当
extern 关键字和初始化都不存在。 (笔记
规则对于函数、类型和模板是不同的。)所以:

extern int a;       //  not a definition
int a;              //  definition
extern int a = 42;  //  definition
int a = 42;         //  definition

规则规定你必须有一个定义,所以你放了一个
定义在源文件中,声明在标头中。

关于链接,声明为变量或引用的符号
如果在名称空间范围内声明,则具有外部链接,但不是
声明为 static,并且不是 const(在 C++11 中也不是 constexpr
或已声明extern。事实上 const 可以给出
变量内部链接有时意味着 extern
必要:

int const a = 42;           //  internal linkage
extern int const a = 42;    //  external linkage

请注意,extern 不必位于同一个声明中:

extern int const a;         //  declaration, in header...
int const a = 42;           //  external linkage, because of
                            //  previous 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. (Note
that the rules are different for functions, types and templates.) So:

extern int a;       //  not a definition
int a;              //  definition
extern int a = 42;  //  definition
int a = 42;         //  definition

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 not const (nor constexpr in C++11)
or has been declared extern. The fact that const can give a
variable internal linkage occasionally means that the extern is
necessary:

int const a = 42;           //  internal linkage
extern int const a = 42;    //  external linkage

Note that the extern doesn't have to be on the same declaration:

extern int const a;         //  declaration, in header...
int const a = 42;           //  external linkage, because of
                            //  previous extern

Still, I've occasionally needed the extern; typically because I want
to 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 an int const a =
42;
, because the template is instantiated with the value 42, and not
the variable a.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文