動態庫是乙個包含可由多個程式同時使用的**和資料的庫,動態庫不是可執行檔案。動態鏈結提供了一種方法,使程序可以呼叫不屬於其可執行**的函式。函式的可執行**位於乙個 動態庫 中,該 動態庫 包含乙個或多個已被編譯、鏈結並與使用它們的程序分開儲存的函式。動態庫 還有助於共享資料和資源。多個應用程式可同時訪問記憶體中單個動態庫 副本的內容。動態庫 是乙個包含可由多個程式同時使用的**和資料的庫。
背景還是編靜態庫那個背景,就是我們乙個庫里提供了多種排序方法,所以源**還是原來那份源**。
sortinte***ce.h
#ifndef sortinte***ce_h
#define sortinte***ce_h
#define inte***ce struct
//排序
inte***ce isort
; //給他們乙個獲取我們子類物件的介面(演算法實現的介面)
extern "c" isort *getcsort(void);
#endif
sort.h
#ifndef sort_h
#define sort_h
#include "sortinte***ce.h"
class csort:public isort
~csort(){}
//氣泡排序
virtual void bubblesort(char* ptr,int len);
//快速排序
virtual void quicksort(char* ptr,int low,int high);
//乙個迴圈排序
virtual void onewhilesort(char* ptr,int len);
};
#endif
sort.cpp
#include "sort.h"
isort* getisort(void)
//氣泡排序
void csort::bubblesort(char* ptr,int len)
} }
return ;
} //快速排序
void csort::quicksort(char* ptr,int low,int high)
tempdata=ptr[temphigh];
ptr[temphigh]=ptr[templow];
ptr[templow]=tempdata;
} quicksort(ptr,low,templow-1);
quicksort(ptr,templow+1,high);
return;
} //乙個循壞排序
void csort::onewhilesort(char* array,int len)
else
} return;
}
好了上面源**已經準備好了,開始編譯。
第一步:將源**編譯成動態庫。
g++ sort.cpp -fpic -shared -o test.so
好像動態庫編譯完成了?那麼接下來就是測試我們的動態庫了
測試**
main.cpp
#include #include #include #include //標頭檔案
#include"sortinte***ce.h"
using namespace std;
int main()
getisort= (isort *(*)(void))dlsym(treedll,"getisort");
const char *dlmsg = dlerror();
if(null != dlmsg)
isort*temprule =getisort();
char a[20];
temprule->quicksort(a,0,19);
for(int i=0;i<20;i++)
return 0;
}
編譯一下可執行檔案。
g++ main.cpp -o a.out -ldl //不加ldl會報未定義的引用
./a.out //試一試
linux下編譯動態庫並呼叫
目錄結構如下 root ubuntu home aaa maketest ls l total 12 rw r r 1 root root 127 sep 24 19 09 main.cpp rw r r 1 root root 99 sep 24 19 18 test.cpp rw r r 1 r...
linux動態庫的編譯與使用
linux下的動態庫以.so為字尾,我也是初次在linux下使用動態庫,寫一點入門步驟,以便以後能方便使用。第一步 編寫linux程式庫 檔案1.動態庫介面檔案 動態庫介面檔案getmaxlen.h ifndef getmaxlen h define getmaxlen h int getmaxle...
Linux 靜態庫與動態庫的生成及呼叫
一,庫 一種可執行 的二進位制形式,可以被載入記憶體執行。其中庫分為靜態庫 動態庫 二,靜態庫和動態庫的區別 1 linux 下靜態庫 名字一般為 lib a利用靜態函式庫編譯成的檔案比較大,因為整個 函式庫的所有資料都會被整合進目標 中,他的優點就顯而易見了,即編譯後的執行程式不需要外部的函式庫支...