動態庫中,關於全域性變數的呼叫

2021-06-25 23:31:09 字數 864 閱讀 7566

測試目的:動態庫中全域性變數的使用方法

測試結果:主程式和庫b共用一套a的動態庫,共用一套全域性變數。

庫a的**:

#includeint pri =0;

void fun()

void change()

編譯:gcc -shared sa.c -o libsa.so

庫b的**:

#include#includevoid fun_b()

編譯:gcc -shared sb.c -o libsb.so

主程式c的**:

#include#include "sa.h"

#include "sb.h"

int main()

編譯:gcc -i../sa -i../sb -o test main.c -lsa -lsb 

注意:動態庫使用的時候ldd test會發現自己編譯的庫無法找到,所以可以把動態庫移動到/lib或/usr/lib目錄下

輸出結果:

i am is a ,pri = 0

*************************==

i am is b

i am is a ,pri = 0

*************************==

*************************==

i am is b

i am is a ,pri = 6

*************************==

i am is a ,pri = 6

關於動態鏈結中的全域性變數

程式設計師自我修養 觀後驗證第二彈。第乙個例子 有三個程式 main.cpp include int global symbol 300 extern void testso1 extern void testso2 int main so1.cpp include int global symbo...

關於 全域性變數

全域性變數一般這樣定義 1。在一類的.cpp中定義 int myint 然後再在要用到的地方的.cpp裡extern int myint 這樣就可以用了。2。在stdafx.cpp中加入 int myint 然後在stdafx.h中加入 extern int myint 這樣定義以後無論在什麼檔案中...

python中關於全域性變數

title date br python中關於全域性變數 2019 09 03 15 58 02 0700 python基礎 python num 1 定義全域性變數 def test1 num 2 print num def test2 print num test1 test2 2 1 並未如所...