VC6.0,c语言extern声明一个static全局变量,链接不通过

yirenrushi 2013-07-22 04:21:12
程序很简单,是测试static和extern的用法,代码如下:
#include<stdio.h>
void f1()
{

extern i;
i=i+1;
printf("%d\n",i);
}
static int i=4;
void main()
{
f1();
f1();
f1();
}
编译通过,链接报错:error LNK2001: unresolved external symbol _i
按理说,static定义静态全局变量,应该是可以被本文件内的函数调用的,我在f1()函数中进行了extern声明,编译都没问题,为什么会出现链接错误呢??另外,把static去掉,直接定义int i,则能正确链接和运行。恳请大神指点迷津啊!



...全文
369 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
proteus2 2013-11-08
  • 打赏
  • 举报
回复
2楼给出的 http://blog.csdn.net/c4501srsy/article/details/7751315上讲了不少 但真实的情况参考cpp2003标准p106页,2011标准在哪一页尚未查找。 This is specifically given as an example in the C++ standard when it's discussing the intricacies of declaring external or internal linkage. It's in section 7.1.1.7, which has this exert: static int b ; // b has internal linkage extern int b ; // b still has internal linkage extern int d ; // d has external linkage static int d ; // error: inconsistent linkage 对于采用extern扩展static变量的作用域,楼主所说VC6不允许(经测试VC6允许),VS2010也允许,但从标准可知,2010这一点并不严格按照标准执行,而codeblocks 2012则比较符合标准。 至于具体的解释,则可以参考 http://stackoverflow.com/questions/14403796/can-a-variable-be-declared-both-static-and-extern 当然也属于他个人理解,感觉还不错 希望对你有所帮助。
proteus2 2013-11-07
  • 打赏
  • 举报
回复
2楼给出的 http://blog.csdn.net/c4501srsy/article/details/7751315上讲了不少 但真实的情况参考cpp2003标准p106页,2011标准在哪一页尚未查找。 This is specifically given as an example in the C++ standard when it's discussing the intricacies of declaring external or internal linkage. It's in section 7.1.1.7, which has this exert: static int b ; // b has internal linkage extern int b ; // b still has internal linkage extern int d ; // d has external linkage static int d ; // error: inconsistent linkage 对于采用extern扩展static变量的作用域,正如楼主所说VC6不允许,但VS2010允许,但从标准可知,2010这一点并不严格按照标准执行,而codeblocks 2012则比较符合标准。 至于具体的解释,则可以参考 http://stackoverflow.com/questions/14403796/can-a-variable-be-declared-both-static-and-extern 当然也属于他个人理解,感觉还不错 希望对你有所帮助。
proteus2 2013-11-07
  • 打赏
  • 举报
回复
2楼给出的 http://blog.csdn.net/c4501srsy/article/details/7751315上讲了不少 但真实的情况参考cpp2003标准p106页,2011标准在哪一页尚未查找。 This is specifically given as an example in the C++ standard when it's discussing the intricacies of declaring external or internal linkage. It's in section 7.1.1.7, which has this exert: static int b ; // b has internal linkage extern int b ; // b still has internal linkage extern int d ; // d has external linkage static int d ; // error: inconsistent linkage 对于采用extern扩展static变量的作用域,正如楼主所说VC6不允许,但VS2010运行,但从标准可知,2010这一点并不严格按照标准执行,而codeblocks 2012则比较符合标准。 至于具体的解释,则可以参考 http://stackoverflow.com/questions/14403796/can-a-variable-be-declared-both-static-and-extern 当然也属于他个人理解,感觉还不错 希望对你有所帮助。
nextseconds 2013-07-24
  • 打赏
  • 举报
回复
extern和static是相反的,同时修饰一个变量i,肯定是不得的,随便去掉一个吧
帅得不敢出门 2013-07-24
  • 打赏
  • 举报
回复
二者有冲突。。。。
hnu_0720 2013-07-24
  • 打赏
  • 举报
回复
还是楼上的正解
JoeBlackzqq 2013-07-23
  • 打赏
  • 举报
回复
何必多此一举呢,将“static int i=4;”提到前面去就好了!
AnYidan 2013-07-22
  • 打赏
  • 举报
回复
lz 很有创意,从未这样用过
一根烂笔头 2013-07-22
  • 打赏
  • 举报
回复
static 就是为了不往外导出的,你又给它个extern。 好比,两人的私密,你(user)非要拿来公布(extern),你媳妇(编译器)不愿意的! 把static去掉即可!
水平不流 2013-07-22
  • 打赏
  • 举报
回复
如果加上static就是静态全局变量,extern只能用于扩展没有用static修饰的全局变量. 要不然就找不到的. extern 和 static 不能用于修饰同一个变量. 这边写得很不错,你去看看 http://blog.csdn.net/c4501srsy/article/details/7751315
图灵狗 2013-07-22
  • 打赏
  • 举报
回复
static变量不会导出变量符号,而extern是依据变量符号进行查找,一般变量xxx的符号名称为_xxx。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧