关于链接规则的疑问 [英] Doubts about Linkage Rules

查看:65
本文介绍了关于链接规则的疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来源: http: //rm-f.net/~orange/devel/specif...t.html#3.1.2.2


本段有两段我可以'完全理解:


1)如果对象或函数的标识符声明

包含存储类说明符extern,标识符具有与文件

范围的标识符的任何可见声明相同的链接。

范围。如果没有带文件范围的可见声明,则

标识符具有外部链接。


特别是:标识符具有相同的链接/ any / / visible /

声明标识符的文件范围"


2)"如果在翻译单元中,同一标识符出现在两者都是内部和外部的联系,行为是未定义的。


i无法想象一个内部和

外部链接...

source: http://rm-f.net/~orange/devel/specif...t.html#3.1.2.2

there are two passages in this paragraph i can''t fully understand:

1) "If the declaration of an identifier for an object or a function
contains the storage-class specifier extern , the identifier has the
same linkage as any visible declaration of the identifier with file
scope. If there is no visible declaration with file scope, the
identifier has external linkage."

in particular: "the identifier has the same linkage as /any/ /visible/
declaration of the identifier with file scope"

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

i can''t imagine an example of an identifier having both internal and
external linkage...

推荐答案

fctk写道:
来源: http://rm-f.net/~orange/ devel / specific ... t.html#3.1.2.2

本段有两段我不能完全理解:

1) 如果声明对象或函数的标识符n
包含存储类说明符extern,该标识符具有与文件
范围的标识符的任何可见声明相同的链接。如果没有带文件范围的可见声明,则
标识符具有外部链接。

特别是:标识符具有与/ any / / visible / 声明具有文件范围的标识符


这意味着以下是foo的合法声明与文件

联动范围:


static int foo;

extern int foo;


这是一个带有外部联动范围的酒吧法律声明:


int bar;

extern int bar;


C89的基本原理说明了这个要求如何允许

实现一个-pass编译器生成中间的

汇编代码。


请注意,C99将规范的* visible *部分说明为

如下(6.2.2):


"对于在

范围内使用存储类说明符extern声明的标识符,其中* previous *声明该标识符是可见的,如果

先前的声明指定了内部或外部链接,则后面声明中标识符的

链接与
相同在先前的声明中指定的
链接灰。如果没有事先声明

可见,或者如果先前的声明没有指定链接,则

标识符具有外部链接。

2 )如果在翻译单元内,同一标识符同时出现内部和外部链接,则行为未定义。

我无法想象一个例子。内部和外部链接的标识符...
source: http://rm-f.net/~orange/devel/specif...t.html#3.1.2.2

there are two passages in this paragraph i can''t fully understand:

1) "If the declaration of an identifier for an object or a function
contains the storage-class specifier extern , the identifier has the
same linkage as any visible declaration of the identifier with file
scope. If there is no visible declaration with file scope, the
identifier has external linkage."

in particular: "the identifier has the same linkage as /any/ /visible/
declaration of the identifier with file scope"
It means that the following is a legal declaration of foo with file
linkage scope:

static int foo;
extern int foo;

and this a legal declaration of bar with external linkage scope:

int bar;
extern int bar;

The rationale document for C89 explains how this requirement allows the
implementation of a one-pass compiler that generates intermediate
assembly code.

Note that C99 clarifies the *visible* part of the specification as
follows (6.2.2):

"For an identifier declared with the storage-class specifier extern in a
scope in which a *prior* declaration of that identifier is visible, if
the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage."
2) "If, within a translation unit, the same identifier appears with both
internal and external linkage, the behavior is undefined."

i can''t imagine an example of an identifier having both internal and
external linkage...




这是不允许的:


static int bar ;

int bar;


-

Diomidis Spinellis

代码质量:开源透视(Addison-Wesley 2006)
http://www.spinellis.gr/codequality


fctk< - >写道:
fctk <-> wrote:

本段有两段我不能完全理解:特别是
[...]:标识符具有相同的链接/ any / / visible /
声明具有文件范围的标识符


标识符的可见性在6.2.1p2中讨论。考虑:


extern int i;

static int i;


第二个声明尚未显示在第一个声明,所以

第一个声明声明我有外部链接。

i无法想象一个内部和外部都有内部和外部标识符的例子联系......

there are two passages in this paragraph i can''t fully understand: [...] in particular: "the identifier has the same linkage as /any/ /visible/
declaration of the identifier with file scope"
Visibility of identifiers is discussed in 6.2.1p2. Consider:

extern int i;
static int i;

The second declaration is not yet visible at the first declaration, so
the first declaration declares i to have external linkage.
i can''t imagine an example of an identifier having both internal and
external linkage...




见上文。 :-)


如果声明被撤销,一切都会很好,因为静态

声明在extern处可见。声明,因此

extern声明将声明i也有内部联系。


-Larry Jones


如果我不能解剖任何东西,我该怎么学习手术呢? - Calvin



See above. :-)

If the declarations were reversed, all would be well since the "static"
declaration would be visible at the "extern" declaration and thus the
"extern" declaration would declare "i" with internal linkage, too.

-Larry Jones

How am I supposed to learn surgery if I can''t dissect anything? -- Calvin


Diomidis Spinellis ha scritto:
Diomidis Spinellis ha scritto:
fctk写道:
2)如果,在一个翻译单元,相同的标识符与内部和外部链接一起出现,行为未定义。

我无法想象一个内部和外部链接...
2) "If, within a translation unit, the same identifier appears with
both internal and external linkage, the behavior is undefined."

i can''t imagine an example of an identifier having both internal and
external linkage...



这是不允许的:

static int bar;
int bar;


This is not allowed:

static int bar;
int bar;




i无法理解为什么会出现问题:


static int bar;

int bar;


让我将前面的例子扩展为:


static int bar; / *首先声明'bar''* /


void f1(无效){

bar;

}


int bar; / *第二次'bar''声明* /


void f2(无效){

bar;

}


首先让我们突出显示

`bar''两个声明的范围;我会在属于第一个声明的

范围的所有行的前面放一个/ * 1 * /,并在所有行前面放一个/ * 2 * /

属于第二个声明的范围:


static int bar; / *首先声明'bar''* /

/ * 1 * /

/ * 1 * / void f1(void){

/ * 1 * / bar;

/ * 1 * /}

/ * 1 * /

int bar; / *第二次申报'bar''* /

/ * 2 * /

/ * 2 * / void f2(无效){

/ * 2 * / bar;

/ * 2 * /}


'bar''的声明都有文件范围,因为它们被放置在

函数定义中任何块之外和任何参数声明列表之外的



请注意,as as鉴于我知道,给定一个标识符'foo'',

声明`foo'的范围和另一个声明的范围

of foo' '*不能*重叠。重叠只适用于

*不同*标识符声明的范围。


此处关联的相关规则是:

1)如果对象或函数的标识符声明具有

文件范围并且包含存储类说明符static,则

标识符具有内部链接。


2)如果对象的标识符声明具有文件范围且没有存储类说明符,则其链接是外部的。


因此对于规则1)所有标识符bar的实例在

的第一个bar声明范围内都有内部链接,而对于规则2)所有

在第二个声明范围内的标识符bar的实例

的bar具有外部链接:


static int bar; / *首先声明'bar''* /

/ * 1 * /

/ * 1 * / void f1(void){

/ * 1 * / bar; / *此实例有内部联动* /

/ * 1 * /}

/ * 1 * /

int bar; / *第二次申报'bar''* /

/ * 2 * /

/ * 2 * / void f2(无效){

/ * 2 * / bar; / *这个实例有外部链接* /

/ * 2 * /}


所以据我所知,没有一些标识符的实例*两个*

内部联动和外部联动。


当然我的推理在某些方面是错误的,但我无法理解/>
在哪里。


抱歉这条长信息。



i can''t understand why there is a problem with:

static int bar;
int bar;

let me expand the previous example to something as:

static int bar; /* first declaration of `bar'' */

void f1(void) {
bar;
}

int bar; /* second declaration of `bar'' */

void f2(void) {
bar;
}

first of all let''s highlight the scopes of the two declarations of
`bar''; i will put an /* 1 */ in front of all lines that belongs to the
scope of the first declaration, and an /* 2 */ in front of all lines
that belong to the scope of the second declaration:

static int bar; /* first declaration of `bar'' */
/* 1 */
/* 1 */ void f1(void) {
/* 1 */ bar;
/* 1 */ }
/* 1 */
int bar; /* second declaration of `bar'' */
/* 2 */
/* 2 */ void f2(void) {
/* 2 */ bar;
/* 2 */ }

both declarations of `bar'' have file-scope, becouse they are put outside
of any block and outside of any list of parameter declarations in a
function definition.

please note that, as far as i know, given a certain identifier `foo'',
the scope of a declaration of `foo'' and the scope of another declaration
of `foo'' *can''t* overlap. overlapping is possible only for the scopes of
*different* identifiers declarations.

the relevant rules here for the linkage are:

1) If the declaration of an identifier for an object or a function has
file scope and contains the storage-class specifier static, the
identifier has internal linkage.

2) If the declaration of an identifier for an object has file scope and
no storage-class specifier, its linkage is external.

so for rule 1) all instances of identifier `bar'' within the scope of the
first declaration of `bar'' have internal-linkage, while for rule 2) all
instances of identifier `bar'' within the scope of the second declaration
of `bar'' have external-linkage:

static int bar; /* first declaration of `bar'' */
/* 1 */
/* 1 */ void f1(void) {
/* 1 */ bar; /* this instance has internal-linkage */
/* 1 */ }
/* 1 */
int bar; /* second declaration of `bar'' */
/* 2 */
/* 2 */ void f2(void) {
/* 2 */ bar; /* this instance has external-linkage */
/* 2 */ }

so as far as i know there is no instance of some identifier with *both*
internal-linkage and external-linkage.

of course my reasoning is wrong in some points, but i can''t understand
where.

sorry for the long message.


这篇关于关于链接规则的疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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