Linux靜態(tài)鏈接庫與模板類的處理方式
在閱讀本文之前,小編先給大家介紹一篇相關(guān)文章:Linux靜態(tài)鏈接庫使用類模板的快速排序算法
大家首先看下以上的文章對理解下面的知識點會有很大的幫助。
當模板遇到靜態(tài)鏈接庫會發(fā)生什么呢。
我們先按照常規(guī)思路去考慮一個靜態(tài)鏈接庫的步驟:
1.將某些功能提取出來,放進一個cpp文件,并將接口或者對外導出的類放在頭文件中
2.gcc -c編譯該文件,生成.o
3.ar命令將.o文件打包成.a,即靜態(tài)鏈接庫
4.編譯main函數(shù),并將該靜態(tài)鏈接庫鏈接,生成可執(zhí)行文件。
OK,按照這個思路,我們將之前寫的快速排序代碼修改后,如下:
lib_test.h:
//lib_test.h //head file of quick sort //users should realise operator > and < #ifndef LIB_TEST_H #define LIB_TEST_H template<class T> class SORT { public: static void myQsort(T a[], int p, int r); static void myQsortNoRecur(T a[], int p, int r); private: static int partition(T a[], int p, int r); static void exchange(T a[], int i, int j); }; #endif
lib_test.cc:
//lib_test.cc #include <iostream> #include <stack> #include"stdlib.h" #include <time.h> #include "lib_test.h" using namespace std; template<class T> void SORT<T>::exchange(T a[], int i, int j) { T temp = a[i]; a[i] = a[j]; a[j] = temp; return; } template<class T> int SORT<T>::partition(T a[],int p,int r) { int i = p; int j = p-1; T ref = a[p]; int refId = p; srand((unsigned)time(NULL)); refId = (rand() % (r-p+1))+ p; //cout<<refId<<endl; ref = a[refId]; for(; i<=r; i++) { if(a[i] < ref) { j++; exchange(a, i, j); if(j == refId) { refId = i; } } } exchange(a, j+1, refId); return j+1; } template<class T> void SORT<T>::myQsort(T a[],int p,int r) { int q = 0; if(p<r) { q = partition(a, p, r); myQsort(a, p, q-1); myQsort(a, p+1, r); } return; } template<class T> void SORT<T>::myQsortNoRecur(T a[], int p, int r) { int start = p; int end = r; int mid = 0; std::stack<int> sortStk; sortStk.push(p); sortStk.push(r); while(!sortStk.empty()) { end = sortStk.top(); sortStk.pop(); start = sortStk.top(); sortStk.pop(); if(start < end) { mid = partition(a, start, end); sortStk.push(start); sortStk.push(mid -1); sortStk.push(mid + 1); sortStk.push(end); } } }
OK,我們嘗試編譯.a靜態(tài)鏈接庫
接下來,只需要將靜態(tài)鏈接庫編入main函數(shù),就算完成了
出問題了,發(fā)現(xiàn)我們編譯的靜態(tài)鏈接庫里面居然沒有這個myQsortNoRecur函數(shù),可是我明明在快速排序這個類SORT里面實現(xiàn)了這個函數(shù)啊。
用nm命令看下:
實實在在的,符號很少,確實沒有我之前寫的函數(shù)。這就奇怪了,今天下午在網(wǎng)上搜了很久,原來是模板類的原因?qū)е碌模?/p>
因為在編譯動態(tài)鏈接庫中,我們并沒有指定template class的type,那么靜態(tài)鏈接庫中自然不知道按照什么type去編譯該class中成員函數(shù)。
參考文獻:在動態(tài)庫和靜態(tài)庫中使用模板(dynamic libraries ,static libraries)
有沒有解決辦法呢?答案是肯定的,只要我們在靜態(tài)鏈接庫中申明一個type,并調(diào)用該指定type的函數(shù),那么靜態(tài)鏈接庫中就有函數(shù)原型了。
我覺得可以把該過程稱為接口的“實例化”過程........
現(xiàn)在把lib_test.cc修改如下:
//lib_test.cc #include <iostream> #include <stack> #include"stdlib.h" #include <time.h> #include "lib_test.h" using namespace std; template<class T> void SORT<T>::exchange(T a[], int i, int j) { T temp = a[i]; a[i] = a[j]; a[j] = temp; return; } template<class T> int SORT<T>::partition(T a[],int p,int r) { int i = p; int j = p-1; T ref = a[p]; int refId = p; srand((unsigned)time(NULL)); refId = (rand() % (r-p+1))+ p; //cout<<refId<<endl; ref = a[refId]; for(; i<=r; i++) { if(a[i] < ref) { j++; exchange(a, i, j); if(j == refId) { refId = i; } } } exchange(a, j+1, refId); return j+1; } template<class T> void SORT<T>::myQsort(T a[],int p,int r) { int q = 0; if(p<r) { q = partition(a, p, r); myQsort(a, p, q-1); myQsort(a, p+1, r); } return; } template<class T> void SORT<T>::myQsortNoRecur(T a[], int p, int r) { int start = p; int end = r; int mid = 0; std::stack<int> sortStk; sortStk.push(p); sortStk.push(r); while(!sortStk.empty()) { end = sortStk.top(); sortStk.pop(); start = sortStk.top(); sortStk.pop(); if(start < end) { mid = partition(a, start, end); sortStk.push(start); sortStk.push(mid -1); sortStk.push(mid + 1); sortStk.push(end); } } } namespace quick_sort_instance { void template_instance() { int a[]={1,2}; SORT<int>::myQsortNoRecur(a, 0, 1); } }
好,重復上面的編譯過程:
這些編譯和執(zhí)行過程就能正常進行了。
但是這種所謂的“實例化”過程有一個明顯的缺點,那就是,本身這個SORT類是一個模板類,可以排序任意類型的數(shù)據(jù),
就本例子而言,只“實例化”了一種int類型的接口。因此當我想排序一個float類型的數(shù)組時,我就必須在.a文件里面再“實例化”一個float接口。
顯然,假如我想把該具有sort功能的類,抽象成一個獨立的模塊,但是我并不知道該.a的用戶想排序的數(shù)據(jù)類型是什么,那么將必須窮舉所有的數(shù)據(jù)類型
這顯然是不可能的。這一局限性不只時模板類,同樣的,模板函數(shù)也是如此。
結(jié)論:最好不要在靜態(tài)鏈接庫中使用模板,同樣的,動態(tài)鏈接庫也一樣。
想到這里,腦子里忽然蹦出來一個想法:C++的STL到底是動態(tài)鏈接韓式靜態(tài)鏈接的呢?STL使用了大量的模板,按照這篇博客在討論的內(nèi)容,似乎是矛盾的。在網(wǎng)上找了半天
參考知乎的大神們是怎么解釋的吧:
https://www.zhihu.com/question/46098144
以上就是本文的全部心得和內(nèi)容,如果大家還有所疑問和建議,可以在下面的留言區(qū)討論。
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。