属于文件作用域的声明在缺省情况下为external链接属性

属于文件作用域的声明在缺省情况下为external链接属性

用一个程序验证:


hello.c

#include<stdio.h>

extern a;

extern b;

int main()

{

printf("%d,%d\n",a,b);

}

a.c

int a=4,b=6


编译:

gcc  a.c  hello.c

输出:

4,6

这个验证是正确的!!!

                                                            

但之前一直测试的代码总是不能通过:

hello.c

#include<stdio.h>
#include<a.h>//唯一不同的地方
extern a;

extern b;

int main()

{

printf("%d,%d\n",a,b);

}
a.h

int a=4,b=6;

通过对比:

所说的external链接属性,应该是指多个c文件之间,可以共享数据。

而使用#include<a.h>之后,属于一个文件。a,b即使第二次在hello.c中定义,仍然为文件内部,所以报错:重定义!!!



你可能感兴趣的:(c,gcc,测试)