[英]error: extern declaration of 'i' follows declaration with no linkage
In the following program, I thought that extern int i;
在下面的程序中,我认为
extern int i;
will change the following i
to refer to the i
defined outside main
:将更改以下
i
以引用在main
之外定义的i
:
#include <stdio.h>
extern int i=1; // warning: 'i' initialized and declared 'extern'
int main()
{
int i=2;
printf("%d\n", i);
extern int i; // error: extern declaration of 'i' follows declaration with no linkage
printf("%d\n", i);
return 0;
}
What is the reason of the "error: extern declaration of 'i' follows declaration with no linkage", where "declaration with no linkage" refers to int i=2;
“错误:'i'的外部声明遵循没有链接的声明”的原因是什么,其中“没有链接的声明”指的是
int i=2;
? ?
After I remove int i=2
in main
,我在
main
删除int i=2
后,
extern int i=1;
extern int i=1;
上的警告“警告:‘i’已初始化并声明为‘extern’” extern int i=1;
also disappear . Thank you for explanations!谢谢你的解释!
#include <stdio.h>
int i=1; // external variable
int main()
{
int i=2; // local variable
printf("%d\n", i); // print local variable i==2
{
extern int i; // point to external variable
printf("%d\n", i); // print external variable i==1
}
return 0;
}
Once you define a variable named i
inside your main
function, the i
at file scope is masked and cannot be accessed (unless you have its address).一旦您在
main
函数中定义了一个名为i
的变量,文件范围内的i
被屏蔽并且无法访问(除非您有它的地址)。
When you later add the declaration extern int i
, this conflicts with the local variable named i
at the same scope since locals can't have external linkage.当您稍后添加声明
extern int i
,这会与同一作用域内名为i
的局部变量发生冲突,因为局部变量不能具有外部链接。 It does not give you access to the global i
.它不给你进入全球
i
。
When you remove the local i
, the extern int i
declaration matches up with the definition at file scope, so there is no error.当您删除本地
i
, extern int i
声明与文件范围内的定义匹配,因此没有错误。 As for the warning on extern int i=1;
至于
extern int i=1;
上的警告extern int i=1;
, that did not go away for me on gcc 4.1.2, so that depends on the compiler. ,这对我来说在 gcc 4.1.2 上并没有消失,所以这取决于编译器。
Question: What is the reason of the "error: extern declaration of 'i' follows declaration with no linkage", where "declaration with no linkage" refers to int i=2;?问题:“错误:'i'的extern声明跟随没有链接的声明”的原因是什么,其中“没有链接的声明”指的是int i = 2;?
Answer: We do not need to use the extern keyword here on line 3 when it is a single file in a program and there is no other file in the same program or other location on the same file where the variable int i has its definition.答:当它是程序中的单个文件并且同一程序中没有其他文件或同一文件中的其他位置(变量int i有其定义)时,我们不需要在第 3 行中使用extern关键字。 There are two main reasons we can use extern keyword in C:
我们可以在 C 中使用extern关键字有两个主要原因:
1. When we want to declare a variable explicitly/globally but without its definition. 1. 当我们想显式/全局声明一个变量但没有定义时。
2. To make the variable globally visible from any other file in a multi-file program or other location of the same file (see Ihdina's exmaple for this scenario). 2. 使变量从多文件程序中的任何其他文件或同一文件的其他位置全局可见(请参阅 Ihdina 的示例以了解这种情况)。
Compiling your code on my system I get the following error,在我的系统上编译您的代码时出现以下错误,
error: extern declaration of 'i' follows non-extern declaration .错误:'i' 的外部声明遵循非外部声明。 which totally makes sense that the compiler detects the extern on line 9 as a duplicate declaration of the same variable int i on line 7.
这完全有道理,编译器将第 9 行的extern检测为第 7 行相同变量int i的重复声明。
Question: After I remove int i=2 in main, the error is gone, the warning "warning: 'i' initialized and declared 'extern'" on extern int i=1;问题:在我删除 main 中的 int i=2 后,错误消失了,extern int i=1 上的警告“警告:'i' 已初始化并声明为 'extern'”; also disappear .
也消失了。 Why is that?
这是为什么?
Answer: After removing the int i=2;答:去掉int i=2后; the error was gone on my system but still I have the following warning message:
错误在我的系统上消失了,但我仍然收到以下警告消息:
warning: 'extern' variable has an initializer [-Wextern-initializer] Basically my system ( Apple LLVM version 8.1.0 (clang-802.0.42) ) does not like the explicit initialization with extern keyword.警告:'extern' 变量有一个初始化器 [-Wextern-initializer]基本上我的系统( Apple LLVM 版本 8.1.0 (clang-802.0.42) )不喜欢使用extern关键字进行显式初始化。 So, you should modify your code as per Ihdina's answer which compiles without error.
因此,您应该根据 Ihdina 的答案修改您的代码,该答案可以正确编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.