具有内部链接的标识符的外部定义
External definition for identifier with internal linkage
我在 C99 标准 6.9.3 中阅读了以下规则
There shall be no more than one external definition for each identifier declared with internal linkage in a translation unit. Moreover, if an identifier declared with internal linkage is used in an expression (other than as a part of the operand of a sizeof operator whose result is an integer constant), there shall be exactly one external definition for the identifier in the translation unit.
我的问题:
(1) 什么是外部定义?
(2) 为什么下面的代码没有违反这条规则?
static int a = 1;
int main()
{
a += 1;
return 0;
}
我认为您混淆了外部定义和使用 extern
关键字的定义。
6.9的语义部分C99/C11都写了定义:
These are described as ‘‘external’’ because they appear outside any
function (and hence have file scope).
在你的例子中,代码没有违反规则,因为你只有一个内部链接对象的外部定义,即:
static int a = 1;
我在 C99 标准 6.9.3 中阅读了以下规则
There shall be no more than one external definition for each identifier declared with internal linkage in a translation unit. Moreover, if an identifier declared with internal linkage is used in an expression (other than as a part of the operand of a sizeof operator whose result is an integer constant), there shall be exactly one external definition for the identifier in the translation unit.
我的问题:
(1) 什么是外部定义?
(2) 为什么下面的代码没有违反这条规则?
static int a = 1;
int main()
{
a += 1;
return 0;
}
我认为您混淆了外部定义和使用 extern
关键字的定义。
6.9的语义部分C99/C11都写了定义:
These are described as ‘‘external’’ because they appear outside any function (and hence have file scope).
在你的例子中,代码没有违反规则,因为你只有一个内部链接对象的外部定义,即:
static int a = 1;