使用外部链接声明的对象的外部定义

External definition of an object declared with external linkage

我对 N2310 C18 的 6.9 p5 中的措辞有点困惑:

If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof or _Alignof 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. 164)

问题:从这段引用中是否可以明显看出程序中某处的外部定义(如果有的话)也应该声明一个带有外部链接的标识符?

正如我强调的那样在整个程序的某处应该只有一个标识符的外部定义。它没有指定定义应使用哪个链接来声明标识符。示例:

tu1.c:

int a = 10;

tu2.c:

static int a = 20;

正式地说,我们在 tu1.c 中声明了标识符 a 的一个外部定义,在 tu2.c 中声明了另一个,因此我们可以将我上面引用的引用应用于此示例。

尽管要表示在不同声明中声明的相同实体标识符,都应使用 6.2.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.

这里不是这种情况。

参见 C11 §6.2.2 Linkages of identifiers:

… There are three kinds of linkage: external, internal, and none.

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.

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.

已强调。

如果用 static 指定文件范围变量,则它具有内部链接并且与具有外部链接的变量的讨论无关。