[英]Confusion about Internal linkage
According to the definition of internal linkage that i have read everywhere states that internal linkage means object is visible at file scope or everywhere in same file. 根据我到处读到的内部链接的定义,内部链接意味着对象在文件范围或同一文件中的任何位置都是可见的。
int main()
{
extern int i;
i = 0; //linker error
}
static int i;
Would you have it any way to make i
visible inside, if its declared after main() without defining before it? 如果它在main()之后声明而没有在它之前定义,你能以任何方式使i
在内部可见吗?
This is not a linking problem, but a compilation problem. 这不是链接问题,而是编译问题。 At the time your main
is compiled, i
is not yet declared. 在你的main
编译时, i
尚未宣布。 So you have to put i
before your main
function in order to compile it. 因此,您必须将i
放在main
函数之前才能编译它。
You're confusing several issues. 你混淆了几个问题。 First, "linkage" concerns symbols, not objects. 首先,“联系”涉及符号,而不是对象。 And secondly, independently of linkage, a symbol must be declared before you can use it. 其次,与链接无关,必须先声明符号才能使用它。 Put the static int i
before main, and there will be no problem. 把main static int i
放在main之前,就没问题了。
Your solution doesn't work as extern
says that the variable is globally visible, but static
says that it is not. 你的解决方案不起作用,因为extern
说变量是全局可见的,但static
表示它不是。
Presumably the linker believes this to be two different variables, and gets confused. 据推测,链接器认为这是两个不同的变量,并且会混淆。
You have to declare all objects before use, but you also have to be consistent. 您必须在使用前声明所有对象,但您还必须保持一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.