function.h
- #ifndef _FUNCTION_H_
- #define _FUNCTION_H_
- void test_extern1_print();
- void test_extern2_print();
- #endif
header.h
- #ifndef _HEADDER_H_
- #define _HEADDER_H_
- extern int test_extern1_val;
- extern int test_extern2_val;
- static int tmp_vale = 3;
- #endif
test_extern1.c
- #include <stdio.h>
- #include "header.h"
- int test_extern1_val = 1;
- void test_extern1_print()
- {
- printf("In test_extern1.c:\n");
- printf("______________________________________________________________________________________________\n");
- printf("| test_extern1_val | address | test_extern2_val | address | tmp_vale | address |\n");
- printf("______________________________________________________________________________________________\n");
- printf("| %d | %p | %d | %p | %d | %p |\n",\
- test_extern1_val,&test_extern1_val,test_extern2_val,&test_extern2_val,tmp_vale,&tmp_vale);
- printf("______________________________________________________________________________________________\n");
- }
test_extern2.c
- #include <stdio.h>
- #include "header.h"
- int test_extern2_val = 2;
- void test_extern2_print()
- {
- printf("In test_extern2.c:\n");
- printf("______________________________________________________________________________________________\n");
- printf("| test_extern1_val | address | test_extern2_val | address | tmp_vale | address |\n");
- printf("______________________________________________________________________________________________\n");
- printf("| %d | %p | %d | %p | %d | %p |\n",\
- test_extern1_val,&test_extern1_val,test_extern2_val,&test_extern2_val,tmp_vale,&tmp_vale);
- printf("______________________________________________________________________________________________\n");
- }
main.c
- #include <stdio.h>
- #include <stdlib.h>
- #include "founctions.h"
- #include "header.h"
- int main()
- {
- system("clear");
- printf("\n\n****************************************** Head *****************************************\n\n");
- test_extern1_print();
- printf("\n\n");
- test_extern2_print();
- printf("\n\n");
- printf("In main.c:\n");
- printf("______________________________________________________________________________________________\n");
- printf("| test_extern1_val | address | test_extern2_val | address | tmp_vale | address |\n");
- printf("______________________________________________________________________________________________\n");
- printf("| %d | %p | %d | %p | %d | %p |\n",\
- test_extern1_val,&test_extern1_val,test_extern2_val,&test_extern2_val,tmp_vale,&tmp_vale);
- printf("______________________________________________________________________________________________\n");
- printf("\n\n");
- printf("\n\n****************************************** Tail *****************************************\n\n");
- return 0;
- }
Makefile
- o_file := test_extern1.o test_extern2.o
- all: main
- main : main.o $(o_file)
- $(CC) main.o $(o_file) -o main
- clean:
- rm -rvf ./*.o main
编译效果:
- ****************************************** Head *****************************************
- In test_extern1.c:
- ______________________________________________________________________________________
- | test_extern1_val | address | test_extern2_val | address | tmp_vale | address |
- ______________________________________________________________________________________
- | 1 | 0x8049ca0 | 2 | 0x8049ca8 | 3 | 0x8049c9c |
- ______________________________________________________________________________________
- In test_extern2.c:
- ______________________________________________________________________________________
- | test_extern1_val | address | test_extern2_val | address | tmp_vale | address |
- ______________________________________________________________________________________
- | 1 | 0x8049ca0 | 2 | 0x8049ca8 | 3 | 0x8049ca4 |
- ______________________________________________________________________________________
- In main.c:
- ______________________________________________________________________________________
- | test_extern1_val | address | test_extern2_val | address | tmp_vale | address |
- ______________________________________________________________________________________
- | 1 |0x8049ca0 | 2 | 0x8049ca8 | 3 | 0x8049c98 |
- ______________________________________________________________________________________
- ****************************************** Tail *****************************************
- [root@localhost test_extern]#
上面注意到,tmp_vale值虽然一样,但是地址是不一样的。
所以,在头文件中定义static变量,代码中好像使用了相同的变量,但是实际上使用的是不同的变量,在每个源文件中都有单独的变量。会造成变量多次定义,造成内存空间的浪费,而且也不是真正的全局变量。应该避免使用这种定义方式
阅读(3348) | 评论(0) | 转发(1) |