将声明转发给静态变量 [英] Forward declarations to static variables

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

问题描述

我如何(便携地)对静态(我的意思是

文件范围)变量进行前向引用?


我去过使用extern多年来,例如:


extern int x;

int foo(无效)

{

返回x ++;

}

static int x = 5;


....和gcc 3.4.4和MSVC 2005甚至没有警告这里有任何错误




但是gcc 4给了我一个错误:


x的静态声明遵循非静态声明


似乎替换extern用静态让它开心。


我一直以为静态表示变量

的定义(而不仅仅是

声明),如果您只想声明变量,则使用extern。现在我

无法理解什么是static int x;实际上是:只是一个声明?

或声明和定义?

请注意我可以删除第二个static int x行和它

仍在编译...


你能解释一下C标准对这个问题的看法以及为什么有些

编译器在gcc 4发出错误时不发出警告?是不是
因为有些编译器默认使用C89而另一个默认为C9X?


谢谢,

Piotr

解决方案



fo*@scene.pl 写于01/19/06 11:16,:

我如何(便携地)对静态(我的意思是
文件范围)变量进行前向引用?

我一直在使用extern多年来,例如:

extern int x;
int foo(void)
{
返回x ++;
}
静态int x = 5;

...而且gcc 3.4.4和MSVC 2005甚至都没有警告这里有什么问题。

但是gcc 4给我一个错误:

'x'的静态声明遵循非静态声明

似乎替换extern用静态让它开心。

我一直以为静态表示变量的定义(而不仅仅是
声明)
如果您只想声明变量,则使用extern。现在我无法理解什么是static int x;实际上是:只是一个声明?
或声明和定义?
请注意我可以删除第二个static int x它仍然可以编译...

你能解释一下C标准对这个问题的看法以及为什么有些
编译器在gcc 4给出时不发出警告一个错误?是不是因为有些编译器默认使用C89而另一个默认使用C9X?




标准的两个版本都说


如果在翻译单元内,同一标识符

同时出现内部和外部链接,则

行为未定义。


C99为6.2.2 / 7,原ANSI为3.1.2.2/7(

ISO重新编号为C90)。


行为未定义暗示了一些事情。

首先,这意味着允许不同的编译器处理

的情况不同;甚至可能会发生单个

编译器无法处理它的问题。第二,它意味着编译器不需要抱怨的
;您可能收到的任何诊断

只是奖金。


结论:不要这样做。决斗声明

无效C,从某种意义上说C对他们没有任何意义。因此,当你编写它们时,你不知道

效果可能是什么,不知道你要求编译器做什么。


- -
Er*********@sun.com


fo*@scene.pl 写道:



我一直认为静态表示变量的定义(而不仅仅是
声明)
如果您只想声明变量,则使用extern。现在我无法理解什么是static int x;实际上是:只是声明?
或声明和定义?




这是一个声明和*暂定*定义。也就是说,如果以后在翻译单元中没有找到实际的定义,那么它将作为定义行事

,但它并不排除实际的定义来自

稍后出现。实际定义包括初始化程序:


static int x; //暂定的定义

static int x = 0; //实际定义


-Larry Jones


任何没有俯卧撑,撞击,烧伤或noogies的游戏都是一个精致的游戏。 - Calvin




< fo*@scene.pl>在消息中写道

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

我如何(便携地)对静态(我的意思是
文件范围)变量进行前向引用?

我一直在使用extern多年来,例如:

extern int x;
int foo(void)
{
返回x ++;
}
静态int x = 5;

...而且gcc 3.4.4和MSVC 2005甚至都没有警告这里有什么问题。

但是gcc 4给我一个错误:

'x'的静态声明遵循非静态声明

似乎替换extern用静态让它开心。

我一直以为静态表示变量的定义(而不仅仅是
声明)
如果您只想声明变量,则使用extern。现在我无法理解什么是static int x;实际上是:只是一个声明?
或声明和定义?
请注意我可以删除第二个static int x它仍然可以编译...

你能解释一下C标准对这个问题的看法以及为什么有些
编译器在gcc 4给出时不发出警告一个错误?是吗
因为有些编译器默认使用C89而另一个默认为C9X?

谢谢,
Piotr




为什么你不能将声明移到文件的顶部?对于

可移植性,你可能需要删除所有对''static''的引用。

''static''对变量应该意味着它没有不会导出到链接器的
。但是,我已经看到一些编译器将''静态''视为

''const''并优化代码...

Rod Pemberton


How do I (portably) make a forward reference to a static (I mean
file-scope) variable?

I''ve been using "extern" for years, for example:

extern int x;
int foo(void)
{
return x++;
}
static int x = 5;

.... and gcc 3.4.4 and MSVC 2005 do not even warn that anything''s wrong
here.

But gcc 4 gives me an error:

static declaration of ''x'' follows non-static declaration

It seems that replacing "extern" with "static" makes it happy.

I always thought that "static" means a definition (and not merely a
declaration) for variables
and if you want to just declare a variable then you use "extern". Now I
cannot understand what "static int x;" actually is: just a declaration?
or declaration and definition?
Please note that I can remove the second "static int x" line and it
still compiles...

Can you explain me what C standards say about this issue and why some
compilers don''t issue a warning while gcc 4 gives an error? Is it
because some compilers default to C89 and the other default to C9X ?

Thanks,
Piotr

解决方案



fo*@scene.pl wrote On 01/19/06 11:16,:

How do I (portably) make a forward reference to a static (I mean
file-scope) variable?

I''ve been using "extern" for years, for example:

extern int x;
int foo(void)
{
return x++;
}
static int x = 5;

... and gcc 3.4.4 and MSVC 2005 do not even warn that anything''s wrong
here.

But gcc 4 gives me an error:

static declaration of ''x'' follows non-static declaration

It seems that replacing "extern" with "static" makes it happy.

I always thought that "static" means a definition (and not merely a
declaration) for variables
and if you want to just declare a variable then you use "extern". Now I
cannot understand what "static int x;" actually is: just a declaration?
or declaration and definition?
Please note that I can remove the second "static int x" line and it
still compiles...

Can you explain me what C standards say about this issue and why some
compilers don''t issue a warning while gcc 4 gives an error? Is it
because some compilers default to C89 and the other default to C9X ?



Both versions of the Standard say

If, within a translation unit, the same identifier
appears with both internal and external linkage, the
behavior is undefined.

That''s 6.2.2/7 in C99 and 3.1.2.2/7 in Original ANSI (which
ISO renumbered to produce C90).

"The behavior is undefined" implies a couple of things.
First, it means that different compilers are allowed to handle
the situation differently; it can even happen that a single
compiler could handle it inconsistently. Second, it means
that the compiler is not required to complain; any diagnostic
you may receive is just a bonus.

The conclusion: Don''t Do That. The duelling declarations
are not valid C, in the sense that C assigns no meaning to
them. Hence, when you write them you have no idea what the
effect might be, no idea what you''re asking the compiler to do.

--
Er*********@sun.com


fo*@scene.pl wrote:



I always thought that "static" means a definition (and not merely a
declaration) for variables
and if you want to just declare a variable then you use "extern". Now I
cannot understand what "static int x;" actually is: just a declaration?
or declaration and definition?



It''s a declaration and a *tentative* definition. That is, if no actual
definition is found later on in the translation unit, then it will act
as a definition, but it doesn''t preclude an actual definition from
appearing later. An actual definition includes an initializer:

static int x; // tentative definition
static int x = 0; // actual definition

-Larry Jones

Any game without push-ups, hits, burns or noogies is a sissy game. -- Calvin



<fo*@scene.pl> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

How do I (portably) make a forward reference to a static (I mean
file-scope) variable?

I''ve been using "extern" for years, for example:

extern int x;
int foo(void)
{
return x++;
}
static int x = 5;

... and gcc 3.4.4 and MSVC 2005 do not even warn that anything''s wrong
here.

But gcc 4 gives me an error:

static declaration of ''x'' follows non-static declaration

It seems that replacing "extern" with "static" makes it happy.

I always thought that "static" means a definition (and not merely a
declaration) for variables
and if you want to just declare a variable then you use "extern". Now I
cannot understand what "static int x;" actually is: just a declaration?
or declaration and definition?
Please note that I can remove the second "static int x" line and it
still compiles...

Can you explain me what C standards say about this issue and why some
compilers don''t issue a warning while gcc 4 gives an error? Is it
because some compilers default to C89 and the other default to C9X ?

Thanks,
Piotr



Why can''t you just move the declaration to the top of the file? For
portability, you''ll probably need to remove all references to ''static''.
''static'' on variables is supposed to mean that it doesn''t get exported to
the linker. However, I''ve seen some compilers that treat ''static'' as
''const'' and optimize away the code...
Rod Pemberton


这篇关于将声明转发给静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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