[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