extern变量和inculde的区别

[root@localhost src2]# cat test.c
int a = 10;
static int b = 100;
[root@localhost src2]# cat main.c
#include <stdio.h>
#include "test.c"
extern int a;
extern int b;

int main(){
  printf("a is %d/n", a);
  printf("b is %d/n", b);
  return 0;
}
[root@localhost src2]# gcc -o main main.c  //static不起作用
[root@localhost src2]# ./main
a is 10
b is 100
[root@localhost src2]# cat main2.c
#include <stdio.h>
#include "test.c"
//extern int a;  //这两句话不起作用
//extern int b;

int main(){
  printf("a is %d/n", a);
  printf("b is %d/n", b);
  return 0;
}
[root@localhost src2]# gcc -o main2 main.c
[root@localhost src2]# ./main2
a is 10
b is 100
[root@localhost src2]# cat main3.c
#include <stdio.h>
//#include "test.c"
extern int a;
extern int b;

int main(){
  printf("a is %d/n", a);
  printf("b is %d/n", b);
  return 0;
}
[root@localhost src2]# gcc -o main3 main3.c test.c
/tmp/ccm4VEgz.o(.text+0x37): In function `main': //找不到b,static起作用了
: undefined reference to `b'
collect2: ld returned 1 exit status
[root@localhost src2]# cat main4.c
#include <stdio.h>
//#include "test.c"
extern int a;
//extern int b;

int main(){
  printf("a is %d/n", a);
//  printf("b is %d/n", b);
  return 0;
}
[root@localhost src2]# gcc -o main4 main4.c test.c
[root@localhost src2]# ./main4
a is 10 //ok,找到了外部的a
[root@localhost src2]# cat main5.c
#include <stdio.h>
#include "test.c"
extern int a;
extern int b;
extern int a;
extern int a; //不管多少个extern a,都不起作用

int main(){
  printf("a is %d/n", a);
  printf("b is %d/n", b);
  return 0;
}
[root@localhost src2]# gcc -o main5 main5.c
[root@localhost src2]# ./main5
a is 10
b is 100
[root@localhost src2]# cat main6.c
#include <stdio.h>
//#include "test.c"
extern int a;
int b;
extern int a;
extern int a;

int a = 10;  //定义了一个a

extern int a;
extern int a;

int main(){
  printf("a is %d/n", a);
  printf("b is %d/n", b);
  return 0;
}
[root@localhost src2]# gcc -o main6 main6.c
/tmp/ccK8arcd.o(.text+0x37): In function `main':
: undefined reference to `b'
collect2: ld returned 1 exit status
[root@localhost src2]# vi main6.c
[root@localhost src2]# gcc -o main6 main6.c
[root@localhost src2]# ./main6
a is 10
b is 0

但是注意,函数不能像变量那样重复声明了:
[root@localhost src2]# cat test.c
#include <stdlib.h>

int a = 10;
static int b = 100;
void test(){
  printf("function/n");
}

static void testStatic(){
  printf("static functuion/n");
}
[root@localhost src2]# cat main5.c
#include <stdio.h>
#include "test.c" //include了test.c
extern int a;
extern int b;
extern int a;
extern int a;
extern test(); // 声明了一个外部函数

int main(){
  printf("a is %d/n", a);
  printf("b is %d/n", b);
  test();
  return 0;
}
[root@localhost src2]# gcc -o main5 main5.c test.c
main5.c:7: error: conflicting types for 'test'
test.c:5: error: previous definition of 'test' was here
[root@localhost src2]# cat main6.c
#include <stdio.h>
//#include "test.c"
extern int a;
int b;
extern int a;
extern int a;

int a = 10;

extern int a;
extern int a;
extern test();  // 声明了一个外部函数
extern test();  // 声明了一个外部函数
void test(){     // 定义了一个函数
  printf("function/nOB");
}

int main(){
  printf("a is %d/n", a);
  printf("b is %d/n", b);
  test();
  return 0;
}
[root@localhost src2]# gcc -o main6 main6.c test.c
main6.c:14: error: conflicting types for 'test'
main6.c:13: error: previous declaration of 'test' was here
main6.c:14: error: conflicting types for 'test'
main6.c:13: error: previous declaration of 'test' was here


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值