为什么一个变量不能两次2个文件在C中定义 [英] Why a variable can't be defined twice in 2 files in C

查看:322
本文介绍了为什么一个变量不能两次2个文件在C中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能有 int类型的; 在2 C文件。我打算这两个结合起来,使可执行文件。
经验告诉我,我不能,但我想找到标准C99说,这其中,并密封我的理解。

Why can't I have int a; in 2 C files. I intend to combine both to make executable. I know from experience that I can't, but I want to find where the standard C99 says this and seal my understanding.

我从 HTTP读取ISO C99标准/www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf 。它说,第42页:

I am reading ISO C99 standard from http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf. It says on page 42:

鉴定网络连接器6.2.2关联

6.2.2 Linkages of identifiers

1的鉴定连接器在不同的声明
  作用域或在同一范围内超过
  一旦可以使指代相同的
  对象或函数通过这个过程被称为
  linkage.There三种
  联动:外部,内部和无

1 An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage.There are three kinds of linkage: external, internal, and none.

2在一组翻译单元和
  构成整个库
  程序的每个声明
  特别是鉴定网络连接器与外部
  联动表示相同对象或
  功能。在一个转换单元,
  一个鉴定科幻器的每个声明与
  内部链接指同一
  对象或函数。每个声明
  不带链接的鉴定连接器的
  表示唯一的实体。

2 In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

3。如果一个文件范围的声明
  鉴定连接器对象或函数
  包含存储类特定连接器
  静态的,鉴定音响器内部有
  联动。

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

4对于申报的鉴定连接器
  在存储类特定连接器的extern
  范围,其中的一个事先声明
  该鉴定网络连接器是可见的,如果在现有
  声明特定网络上课内部或
  外部链接,的联动
  在后来的声明鉴定连接器是
  相同的联动特定网络连接编辑在
  事先声明。如果没有事先
  声明是可见的,或者如果
  事先声明特定网络ES没有任何联系,
  那么鉴定连接器具有外部
  联动。

4 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.

5如果鉴定音响器的声明
  一个函数没有存储类
  特定连接器,其联动确定
  完全一样,如果它用声明
  存储类特定连接器extern.If
  一个鉴定网络连接器的声明为
  对象有网络连接文件的范围和无
  存储类特定连接器,其联动性
  外部的。

5 If the declaration of an identifier for a function has no storage-class specifier,its linkage is determined exactly as if it were declared with the storage-class specifier extern.If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

在阅读完之后,它看起来,如果我喜欢声明说 int类型的变量; 2源文件即可。那么都有外部链接按规则5和4,然后按第2条,既应该是指同一个对象。那么为什么编译器创建的问题。凡在标准则暗示,我们不能宣布像这样2的源文件,这应该抛出编译错误。首先,其中的标准,它说,int类型的一个定义,然后在那里说,定义两个实例是不能接受的。我知道这是不是从我的经验允许的,但它是对我非常有用的,如果我能找到这样的标准,封我的理解。

After reading this it looks that if I declare a variable like say int a; in 2 source files. then both have external linkage as per rule 5 and 4. and then as per rule 2, both should refer to the same object. Then why does the compiler create problem. Where in the standard it is hinted that we can't declare like this in 2 source files and this should throw compilation error. Firstly, where in the standard, it says that int a is a definition, and then where it says that 2 instances of definitions are not acceptable. I know that it is not allowed from my experience, but it would be very useful to me, if I can find this in the standard and seal my understanding.

不要从量相结合这一规则标准以下摘录?或者我已经错过了胶

一个特定的声明上课科幻的
  间pretation和一组属性
  鉴定的科幻器。的A德网络nition
  鉴定连接器是一个宣言
  鉴定网络连接器是: - 对于一个对象,原因
  存储被保留用于
  目的; - 对于功能,包括
  函数体; - 对于一个枚举
  常量或typedef名称,是
  该鉴定音响器的(只)的声明。

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: —for an object, causes storage to be reserved for that object; —for a function, includes the function body; —for an enumeration constant or typedef name, is the (only) declaration of the identifier.

正如在5.1.1.1所讨论的,单元的
  之后preprocessing程序文本是
  翻译单元,它由一个
  外部声明的顺序。
  这些被描述为'外部''
  因为theyappear外anyfunction
  (因而有网络连接文件的范围)。如
  在6.7讨论的,声明
  也导致存储为保留
  物体或通过指定的功能
  鉴定连接器是一个去连接nition。

As discussed in 5.1.1.1, the unit of program text after preprocessing is a translation unit, which consists of a sequence of external declarations. These are described as ‘‘external’’ because theyappear outside anyfunction (and hence have file scope). As discussed in 6.7, a declaration that also causes storage to be reserved for an object or a function named by the identifier is a definition.

外部德网络nition的是,同样是去网络nition外部声明
  的函数的(比内嵌其他
  德网络nition)或对象。如果
  鉴定连接器与外部声明
  联动在离pression使用
  (不是作为操作数的一部分的其他
  一个sizeof操作符,其结果是
  在整型常量),某处
  整个程序应当有确切
  一个外部德网络nition为
  鉴定网络连接器;否则,应
  不超过一个。

感谢。

推荐答案

我想你需要6.9.2 / 2:

I think you need 6.9.2/2:

标识符的对象具有文件范围没有初始化,且无存储类说明或存储类说明的声明静态,构成了的暂定定义的。如果翻译单元包含一个标识符的一个或多个试探性的定义,翻译单元包含该标识符的外部定义,则行为是完全一样的,如果翻译单元包含标识符的文件范围内声明,与复合类型的翻译单元的端部,具有一个初始化等于0

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.

和6.9 / 5:

这是外部定义的是,这也是一个函数的定义外部声明
  (比内联定义其他)或对象。如果一个标识符与外部声明
  联动在离pression比使用(其他为的sizeof 运算符的操作数的一部分
  ,其结果是一个整型常量),地方整个程序应当有
  使标识符恰好一个外部定义;否则,应不超过一个。

An external definition is an external declaration that is also a definition of a function (other than an inline definition) or an object. If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one.

基本上, int类型的; 暂定定义的。你可以有多个暂定定义在一个翻译单元,但效果是一样的有一个非暂定外部定义(如像 int类型的= 0; )。有更多的在程序中外部链接的对象的一个​​定义是违反6.9 / 5。

Basically, int a; is a tentative definition. You can have multiple tentative definitions in a single translation unit but the effect is the same as having one non-tentative external definition (e.g. something like int a = 0;). Having more that one definition of an object with external linkage in a program is a violation of 6.9/5.

请注意,这是一个共同延伸,以允许以便在长最多一个对象的一个​​以上的外部定义仅一个被初始化并定义一致(见J.5.11)

Note that it is a "common extension" to allow more than one external definitions of an object so long as at most only one is initialized and the definitions agree (see J.5.11).

这篇关于为什么一个变量不能两次2个文件在C中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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