两个不同的文件相互引用全局变量
生活随笔
收集整理的這篇文章主要介紹了
两个不同的文件相互引用全局变量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ? ? ? 一個工程下有很多文件,都是用makefile進行管理的,但是一個文件怎么去調用另外一個文件中的全局變量呢?這個工作也涉及了很多,簡單實用。
?
[mapan@localhost CSDN]$ ls a.c b.c b.h [mapan@localhost CSDN]$ cat a.c #include<stdio.h> #include "b.h"int main() {int num=getTest();printf("num=%d\n",num);return 0; }[mapan@localhost CSDN]$ cat b.c #include<stdio.h>int test = 1;int getTest(void) {return test; } [mapan@localhost CSDN]$ cat b.h #ifndef __B_H__ #define __B_H__int getTest(void);#endif [mapan@localhost CSDN]$ gcc a.c b.c b.h [mapan@localhost CSDN]$ ./a.out num=1 [mapan@localhost CSDN]$?
一目了然,感覺有點意思。當然用extern也行。
[mapan@localhost CSDN]$ ls a.c b.c b.h [mapan@localhost CSDN]$ cat a.c #include<stdio.h> //#include "b.h"int main() {extern int getTest();int num=getTest();printf("num=%d\n",num);return 0; }[mapan@localhost CSDN]$ gcc a.c b.c b.h [mapan@localhost CSDN]$ ./a.out num=1 [mapan@localhost CSDN]$2種選一種即可。
?
?
?
?
?
總結
以上是生活随笔為你收集整理的两个不同的文件相互引用全局变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: int main(int argc, c
- 下一篇: bind函数