日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

两个不同的文件相互引用全局变量

發布時間:2025/6/15 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 两个不同的文件相互引用全局变量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? ? ? ? 一個工程下有很多文件,都是用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種選一種即可。

?

?

?

?

?

總結

以上是生活随笔為你收集整理的两个不同的文件相互引用全局变量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。