多个全局变量定义 [英] multiple global variable definitions

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

问题描述

假设我在两个不同的文件中声明一个全局变量


int g;


说ac有main()和bc


当我编译它们以在Redhat Linux中使用gcc构建可执行文件

时使用命令


gcc -std = c99 -pedantic -Wall -Wextra ac bc


没有链接器错误。


我认为应该有链接器错误因为两个

的定义是相同的全局变量。


我哪里错了?

Suppose I declare a global variable

int g;

in two different files say a.c which has main() and b.c

When I compile them to build an executable under gcc in Redhat Linux
with the command

gcc -std=c99 -pedantic -Wall -Wextra a.c b.c

there is no linker error.

I thought there should be linker error because of two definitions for
the same global variable.

Where am I going wrong ?

推荐答案



< su ************** @ yahoo.comwrote in message

news:11 **********************@e65g2000hsc.googlegr oups.com ...

<su**************@yahoo.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...

假设我声明一个全局变量


int g;


在两个不同的文件中说ac有main()和bc


当我编译它们以在Redhat Linux中的gcc下构建可执行文件时

命令


gcc -std = c99 -pedantic -Wall -Wextra ac bc


没有链接器错误。


我认为应该有链接器错误,因为

的两个定义是相同的全局变量。


其中我错了吗?
Suppose I declare a global variable

int g;

in two different files say a.c which has main() and b.c

When I compile them to build an executable under gcc in Redhat Linux
with the command

gcc -std=c99 -pedantic -Wall -Wextra a.c b.c

there is no linker error.

I thought there should be linker error because of two definitions for
the same global variable.

Where am I going wrong ?



.c文件中声明的全局变量具有文件范围。你有两个

不同的变量,与声明相同的int i相同。作为两个不同功能的本地

变量。它们具有相同的名称,但

的范围不重叠。


如果一个文件将变量声明为
,则只有一个变量
是extern。

Global variables declared in the .c file have file scope. You have two
different variables, the same as declaring the same "int i" as a local
variable in two different functions. They have the same name, but
do not overlap in scope.

There would be only one variable if one file declared the variable to
be"extern".


su ************** @ yahoo.com ,印度说:
su**************@yahoo.com, India said:

假设我宣布全球变量


int g;


在两个不同的文件中说ac有main()和bc


当我编译它们以在Redhat Linux中使用gcc构建可执行文件

时使用命令


gcc -std = c99 -pedantic -Wall -Wextra ac bc


没有链接器错误。


我认为应该有链接器错误,因为
$ b的两个定义$ b相同的全局变量。


我哪里错了?
Suppose I declare a global variable

int g;

in two different files say a.c which has main() and b.c

When I compile them to build an executable under gcc in Redhat Linux
with the command

gcc -std=c99 -pedantic -Wall -Wextra a.c b.c

there is no linker error.

I thought there should be linker error because of two definitions for
the same global variable.

Where am I going wrong ?



他们只是暂定的定义,而不是实际定义。但是,

然而,没有暂定初始化这样的东西,所以将它们改为

两者:


int g = 0;


并观察编译器(或链接器)的阻塞。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

They are only "tentative" definitions, not actual definitions. There is,
however, no such thing as a "tentative initialisation", so change them
both to:

int g = 0;

and watch the compiler (or rather the linker) choke.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Richard Heathfield写道:
Richard Heathfield wrote:

> su ************** @ yahoo.com,印度说:
>su**************@yahoo.com, India said:

> ;假设我在两个不同的文件中声明了一个全局变量
int g;
说ac有main()和bc
没有链接器错误。

我以为应该有链接器错误,因为同一个全局变量有两个定义。
>Suppose I declare a global variable
int g;
in two different files say a.c which has main() and b.c
there is no linker error.

I thought there should be linker error because of two definitions for
the same global variable.


它们只是暂定的定义,而不是实际定义。然而,没有暂定初始化之类的东西,所以将它们改为:

int g = 0;
并观察编译器(或更确切地说是链接器)的阻塞。


They are only "tentative" definitions, not actual definitions. There is,
however, no such thing as a "tentative initialisation", so change them
both to:

int g = 0;

and watch the compiler (or rather the linker) choke.



试图理解标准,来自N1124:


" 6。语言

....

6.9外部定义

....

6.9.2外部对象定义

语义

....

2对象标识符的声明

有文件范围没有初始化程序,并且没有存储类说明符或

存储类说明符static的
构成了一个

暂定定义。如果翻译单元

包含一个或多个

标识符的暂定定义,并且翻译单元不包含该标识符的

外部定义,然后

行为就像翻译单元

包含一个

标识符的文件范围声明一样,其复合类型为结束

的翻译单元,初始化程序等于

到0.


虽然我期望的行为是OP

描述,上面的段落似乎与

相矛盾:


int g; 完全是 int g = 0;


我缺少什么?


此外,有人可以澄清含
$的含义b $ b

翻译单元末尾的复合类型 (我不确定我是否因为一个不明确的文字而苦苦挣扎,因为英语不是我母语的事实

。)

>
Roberto Waltman


[请回复小组,

返回地址无效]

Trying to understand the standard, from N1124:

"6. Language
....
6.9 External Definitions
....
6.9.2 External object definitions
Semantics
....
2 A declaration of an identifier for an object
that has file scope without an initializer, and
without a storage-class specifier or with the
storage-class specifier static, constitutes a
tentative definition. If a translation unit
contains one or more tentative definitions for an
identifier, and the translation unit contains no
external definition for that identifier, then
the behavior is exactly as if the translation unit
contains a file scope declaration of that
identifier, with the composite type as of the end
of the translation unit, with an initializer equal
to 0."

Although I expect the behavior that the OP
described, the paragraph above seems to contradict
it:

int g; "is exactly as" int g = 0;

What I am missing?

Also, can somebody clarify the meaning of "with
the composite type as of the end of the
translation unit" (I am not sure if I am
struggling with an unclear text, of with the fact
that English is not my native language.)

Roberto Waltman

[ Please reply to the group,
return address is invalid ]

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

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