"&的extern QUOT;含义 [英] "extern" meaning

查看:75
本文介绍了"&的extern QUOT;含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在阅读C:A参考手册第4版,我迷失了

extern。它说没有指定

存储类说明符的全局对象将具有extern作为默认存储类

说明符。我的(小)C经验告诉我一个对象是

" extern"是让链接器知道该对象引用某处定义的

对象,并且这个某处是对象没有

存储类说明符extern。那就是:

< ------------文件A ------------>

int A;

...

A = 10;

< ------------文件B - ----------->

extern int A;

if(A)

...


但是如果默认存储类说明符是extern,那么

变量int A实际上是extern int A。然后谁是明确的

出现?

如果有错误概念,请纠正我。

解决方案

ccwork写道:

大家好,
我正在阅读C:A参考手册第4版,我迷失了
extern。它表示没有指定
存储类说明符的全局对象将具有extern。作为默认的
存储类说明符。我的(小)C经验告诉我一个具有
extern的对象。是让链接器知道对象正在引用某个地方定义的对象
,而这个某处是对象没有
具有存储类说明符extern。那就是:
< ------------文件A ------------>
int A;
.. 。
A = 10;
< ------------文件B ------------>
extern int A ;
如果(A)
...

但是如果默认存储类说明符是extern,则
变量int A实际上是extern int A。然后谁是
定义出现?
如果有错误的概念,请纠正我。




''extern''指定外部链接对于标识符,仅此而已。

它使链接器知道标识符,以便可以引用它。


变量A的定义出现在文件中; A> ;.


P.Krumins


ccwork写道:

I正在阅读C:参考手册第4版,我迷失了
extern。它表示没有指定
存储类说明符的全局对象将具有extern。作为默认的存储类
说明符。我的(小)C经验告诉我一个具有
extern的对象。是让链接器知道该对象正在引用在某处定义的对象,并且这个某处是对象没有存储类说明符extern。那就是:
< ------------文件A ------------>
int A;
.. 。
A = 10;
< ------------文件B ------------>
extern int A ;
如果(A)
...

但是如果默认存储类说明符是extern,则
变量int A实际上是extern int A。那么谁是明确的?
如果有错误的概念,请纠正我。




C语言并不依赖关于链接器的概念。没有

链接器在C.语言层面,标识符的外部链接

意味着一件事 - 这个标识符指的是所有翻译单元中的同一个对象

(或函数)。这个要求是如何在实践中实现的(使用链接器或其他东西)

是一个实现细节,一个完全不同的故事,与

语言本身。


如果你真的想在导出/导入方面考虑这个问题

访问该对象想一想:当''extern''包含在某个对象的
a _definition_中时,它会导出。相应的标识符

到外界(即其他翻译单位),但当''extern''

包含在一个对象的仅仅_declaration_中时,它进口"来自外界的

标识符。


在你的情况下''int A''在文件中''''是标识符的定义' 'A''

附加在'int'类型的对象上。默认情况下,此标识符具有外部

链接,这意味着标识符A被导出,可以从其他翻译单元访问
。为了访问这个对象,其他翻译单位必须导入。这个标识符为它提供了一个

''extern''_ decalala_。这正是你在文件''B'中所拥有的。


-

祝你好运,

Andrey Tarasevich


Andrey Tarasevich写道:


C语言并不依赖于链接器的概念。


[我认为你并不是要加上额外的''不''。

没有链接器在C.


那么翻译阶段8到底是什么?

在语言层面,标识符的外部链接仅仅意味着
一件事 - 这个标识符指的是所有翻译单元中的同一个对象(或
函数)。如何在实践中实现这一要求(使用链接器或其他东西)是一个实现细节,一个完全不同的故事,与语言本身无关。



库组件已链接......


如何实现链接_without_链接器?


即使C语言解释器也必须有一个链接器,就像它们必须具有

a函数调用栈一样。


-

彼得


Hi all,
I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class
specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the
object defined in somewhere, and this "somewhere" object does not have
the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting
occurence?
Please correct me if there is a mis-concept.

解决方案

ccwork wrote:

Hi all,
I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting occurence?
Please correct me if there is a mis-concept.



''extern'' specifies external linkage for an identifier, nothing more.
It makes the identifier known to the linker so it could be referenced.

The definition of variable A occurs in file "A".

P.Krumins


ccwork wrote:

I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class
specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the
object defined in somewhere, and this "somewhere" object does not have
the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting
occurence?
Please correct me if there is a mis-concept.



C language doesn''t not rely on the concept of "linker". There''s no
"linker" in C. At language level, external linkage of an identifier
means just one thing - that this identifier refers to the same object
(or function) in all translation units. How this requirement is
implemented in practice (using that "linker" thingy or something else)
is an implementation detail, a completely different story, irrelevant to
the language itself.

If you really want to think about this in terms of "exporting/importing"
access to the object, think of it this way: when ''extern'' is included in
a _definition_ of some object, it "exports" the corresponding identifier
to the outside world (i.e. other translation units), but when ''extern''
is included in a mere _declaration_ of an object, it "imports" the
identifier from the outside world.

In your case ''int A'' in file ''A'' is a definition of an identifier ''A''
attached to an object of type ''int''. This identifier has external
linkage by default, meaning that identifier ''A'' is "exported", made
accessible from other translation units. In order to access this object
other translation units have to "import" this identifier by providing an
''extern'' _declaration_ for it. That''s exactly what you have in file ''B''.

--
Best regards,
Andrey Tarasevich


Andrey Tarasevich wrote:


C language doesn''t not rely on the concept of "linker".
[I presume you didn''t mean to add the extra ''not''.]
There''s no "linker" in C.
So what''s translation phase 8 all about then?
At language level, external linkage of an identifier means just
one thing - that this identifier refers to the same object (or
function) in all translation units. How this requirement is
implemented in practice (using that "linker" thingy or something
else) is an implementation detail, a completely different story,
irrelevant to the language itself.



"Library components are linked..."

How does an implementation link _without_ a linker?

Even C interpreters must have a linker, just as they must have
a function call stack.

--
Peter


这篇关于&QUOT;&的extern QUOT;含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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