块作用域链接 C 标准
以下标识符没有链接:声明为对象或函数之外的任何标识符;声明为函数参数的标识符; 未使用存储类说明符 extern 声明的对象的块作用域标识符。
{
static int a; //no linkage
}
对于在先前声明过的作用域中使用存储类说明符 extern 声明的标识符该标识符是可见的,如果先前的声明指定了内部或外部链接,则后面的声明中的标识符的链接与先前的声明中指定的链接相同。如果没有可见的先前声明,或者如果先前声明未指定链接,则标识符具有外部链接。
{
static int a; //no linkage
extern int a; //a should get external linkage, no?
}
GCC 错误:以下声明的外部声明没有链接
有人可以解释一下为什么我会收到此错误吗?
谢谢
The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.
{
static int a; //no linkage
}
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.
{
static int a; //no linkage
extern int a; //a should get external linkage, no?
}
GCC error: extern declaration of a follows declaration with no linkage
Can somebody explain me why do I get this error?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的假设是正确的:
a
的第二个声明具有外部链接。但是,您会收到错误,因为您的代码违反了第 6.7 节中的约束:也就是说,一旦您将
a
声明为没有链接,您就无法在同一范围内再次重新声明它。调用此规则的一个有效示例是:
Your supposition is correct: the second declaration of
a
has external linkage. However, you get an error because your code violates a constraint in §6.7:That is, once you've declared
a
to have no linkage, you can't redeclare it again in the same scope.A valid example of this rule being invoked is:
意味着
并且不
这是令人困惑和不明确的;这不是编写标准的通常方法......
means
and not
This is confusing and ambiguous; not the usual way to write a standard...