22

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.

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 Linkages of identifiers

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

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.

Do the following excerpts from the standard in combination amount to this rule? or I have missed that glue? :

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.

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.

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.

Thanks.

4

1 に答える 1

18

6.9.2/2 が必要だと思います:

ファイル スコープを持つオブジェクトの識別子の宣言で、初期化子がなく、ストレージ クラス指定子がなくても、ストレージ クラス指定子が指定されていても、暫定的な定義staticが構成されます。翻訳単位に識別子の 1 つ以上の暫定的な定義が含まれており、翻訳単位にその識別子の外部定義が含まれていない場合、動作は、翻訳単位にその識別子のファイル スコープ宣言が含まれているかのように、複合型は次のようになります。 0 に等しい初期化子を使用して、翻訳単位の末尾を指定します。

と 6.9/5:

外部定義は、関数 (インライン定義以外) またはオブジェクトの定義でもある外部宣言です。sizeof外部リンケージで宣言された識別子が式で使用される場合 (結果が整数定数である演算子のオペランドの一部として以外)、プログラム全体のどこかに、識別子の外部定義が 1 つだけ存在する必要があります。それ以外の場合は、1 つしか存在しないものとします。

基本的にint a;暫定的な定義です。1 つの翻訳単位に複数の暫定的な定義を含めることができますが、効果は 1 つの非暫定的な外部定義 (たとえば のようなものint a = 0;) を持つことと同じです。プログラム内で外部リンケージを持つオブジェクトの定義を複数持つことは、6.9/5 の違反です。

せいぜい1つだけが初期化され、定義が一致する限り、オブジェクトの複数の外部定義を許可するのは「共通拡張」であることに注意してください(J.5.11を参照)。

于 2011-02-14T09:14:39.437 に答える