static 遇到extern,会如何?

苦逼码农 2016-04-14 11:42:59
一个项目中用到了一个extern变量,,


然后,某些.c文件中。也定义了同名变量,

同名变量用static修饰,

请问该extern变量,,会对static 变量造成什么干扰?
...全文
1807 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Simia_0 2017-11-05
  • 打赏
  • 举报
回复
引用 7 楼 qq423399099 的回复:
楼主你可以自己试一下呗 a.c如果没有包含xx.h,那肯定没关系,这个显而易见 a.c如果包含xx.h,也没关系,别的文件中的全局变量,会被本文件的static覆盖 (楼主也可以看看强符号,弱符号加深理解) static 关键字可以避免编译器的小(?)聪明. 明确不需要被其他文件extern的全局变量,显式的告诉编译器,表extern我
请问“a.c如果包含xx.h, 会被本文件的static覆盖”在哪里可以求证呢? 可以推荐相关资料吗
均陵鼠侠 2016-04-14
  • 打赏
  • 举报
回复
引用 3 楼 qq423399099 的回复:
[quote=引用 2 楼 sholber 的回复:] [quote=引用 1 楼 qq423399099 的回复:] static 声明的全局变量只能在当前源文件中使用; extern 不是定义,是引入(声明)在其它源文件中定义的非 static 全局变量;
啥?其它源文件?你的意思是我这样不行咯:
int x;

void f (void)
{
    extern int x;
    if (/* ... */)
    {
        extern int x;
     }
}
[/quote] 楼主是要干嘛?[/quote] 我就是想问,这3个x是不是同一个。
小灸舞 版主 2016-04-14
  • 打赏
  • 举报
回复
引用 2 楼 sholber 的回复:
[quote=引用 1 楼 qq423399099 的回复:] static 声明的全局变量只能在当前源文件中使用; extern 不是定义,是引入(声明)在其它源文件中定义的非 static 全局变量;
啥?其它源文件?你的意思是我这样不行咯:
int x;

void f (void)
{
    extern int x;
    if (/* ... */)
    {
        extern int x;
     }
}
[/quote] 楼主是要干嘛?
均陵鼠侠 2016-04-14
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
static 声明的全局变量只能在当前源文件中使用; extern 不是定义,是引入(声明)在其它源文件中定义的非 static 全局变量;
啥?其它源文件?你的意思是我这样不行咯:
int x;

void f (void)
{
    extern int x;
    if (/* ... */)
    {
        extern int x;
     }
}
小灸舞 版主 2016-04-14
  • 打赏
  • 举报
回复
static 声明的全局变量只能在当前源文件中使用; extern 不是定义,是引入(声明)在其它源文件中定义的非 static 全局变量;
iyomumx 2016-04-14
  • 打赏
  • 举报
回复
引用 11 楼 u012879787 的回复:
什么叫先出现static, 后出现extern, 先出现extern后出现 static? 是不是这么理解: 1. statci int data_handle; #include" xx.h" //头文件里包含extern 的申明 2. #include "xx.h" staic int data_handle;
和头文件没什么关系,你可以试试:
$ echo 'static int i;' > si.c
$ echo 'extern int i;' > ei.c
$ cat si.c ei.c > sei.c
$ cat ei.c si.c > esi.c
$ gcc - c sei.c
$ gcc - c esi.c
esi.c:2 : 12 : error : static declaration of ‘i’ follows non - static declaration
static int i;
^
esi.c:1 : 12 : note : previous declaration of ‘i’ was here
extern int i;
^
苦逼码农 2016-04-14
  • 打赏
  • 举报
回复
引用 10 楼 iyomumx 的回复:
[quote=引用 9 楼 bsnry 的回复:] 你引用的帖子丢失了?
引用的是 C 语言标准(C11草案)[/quote] 什么叫先出现static, 后出现extern, 先出现extern后出现 static? 是不是这么理解: 1. statci int data_handle; #include" xx.h" //头文件里包含extern 的申明 2. #include "xx.h" staic int data_handle;
iyomumx 2016-04-14
  • 打赏
  • 举报
回复
引用 9 楼 bsnry 的回复:
你引用的帖子丢失了?
引用的是 C 语言标准(C11草案)
bsnry 2016-04-14
  • 打赏
  • 举报
回复
引用 8 楼 iyomumx 的回复:
引用
If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.
引用
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.
所以先出现 extern 后出现 static 是不行的,先出现 static 后出现 extern 则为 static
你引用的帖子丢失了?
iyomumx 2016-04-14
  • 打赏
  • 举报
回复
引用
If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.
引用
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.
所以先出现 extern 后出现 static 是不行的,先出现 static 后出现 extern 则为 static
小灸舞 版主 2016-04-14
  • 打赏
  • 举报
回复
楼主你可以自己试一下呗 a.c如果没有包含xx.h,那肯定没关系,这个显而易见 a.c如果包含xx.h,也没关系,别的文件中的全局变量,会被本文件的static覆盖 (楼主也可以看看强符号,弱符号加深理解) static 关键字可以避免编译器的小(?)聪明. 明确不需要被其他文件extern的全局变量,显式的告诉编译器,表extern我
cocoabird 2016-04-14
  • 打赏
  • 举报
回复
引用 5 楼 u012879787 的回复:
xx.h里有 extern int data_handle; a.c里有static int data_handle, 其他.c也用到了data_handle, 其他.c里有没有用static修饰该变量,我没有关注!!!! 我的问题是: 如果a.c包含 xx.h,那么 data_handle; 会有什么后果? a.c如果没有包含xx.h,那么data_handle又有什么后果? 我想对a.c里的static int data_handle;重命名,由于没有搞定这语法,所以不敢随意重命名。
xx.h里有 extern int data_handle; a.c里有static int data_handle, 两个文件中的data_handle 没有关系, a.c中的 data_handle只是在a.c中起作用,不能被其它文件访问,也不影响xx.h中外部变量data_handle的值
苦逼码农 2016-04-14
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
static 声明的全局变量只能在当前源文件中使用; extern 不是定义,是引入(声明)在其它源文件中定义的非 static 全局变量;
我刚才回复你了,结果回复不见了。再次回复。 xx.h里有 extern int data_handle; a.c里有static int data_handle, 其他.c也用到了data_handle, 其他.c里有没有用static修饰该变量,我没有关注!!!! 我的问题是: 如果a.c包含 xx.h,那么 data_handle; 会有什么后果? a.c如果没有包含xx.h,那么data_handle又有什么后果? 我想对a.c里的static int data_handle;重命名,由于没有搞定这语法,所以不敢随意重命名。

69,371

社区成员

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

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