在閱讀本文之前,小編先給大家介紹一篇相關文章:linux靜態鏈接庫使用類模板的快速排序算法
大家首先看下以上的文章對理解下面的知識點會有很大的幫助。
當模板遇到靜態鏈接庫會發生什么呢。
我們先按照常規思路去考慮一個靜態鏈接庫的步驟:
1.將某些功能提取出來,放進一個cpp文件,并將接口或者對外導出的類放在頭文件中
2.gcc -c編譯該文件,生成.o
3.ar命令將.o文件打包成.a,即靜態鏈接庫
4.編譯main函數,并將該靜態鏈接庫鏈接,生成可執行文件。
ok,按照這個思路,我們將之前寫的快速排序代碼修改后,如下:
lib_test.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
//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靜態鏈接庫
接下來,只需要將靜態鏈接庫編入main函數,就算完成了
出問題了,發現我們編譯的靜態鏈接庫里面居然沒有這個myqsortnorecur函數,可是我明明在快速排序這個類sort里面實現了這個函數啊。
用nm命令看下:
實實在在的,符號很少,確實沒有我之前寫的函數。這就奇怪了,今天下午在網上搜了很久,原來是模板類的原因導致的:
因為在編譯動態鏈接庫中,我們并沒有指定template class的type,那么靜態鏈接庫中自然不知道按照什么type去編譯該class中成員函數。
參考文獻:
有沒有解決辦法呢?答案是肯定的,只要我們在靜態鏈接庫中申明一個type,并調用該指定type的函數,那么靜態鏈接庫中就有函數原型了。
我覺得可以把該過程稱為接口的“實例化”過程........
現在把lib_test.cc修改如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
//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); } } |
好,重復上面的編譯過程:
這些編譯和執行過程就能正常進行了。
但是這種所謂的“實例化”過程有一個明顯的缺點,那就是,本身這個sort類是一個模板類,可以排序任意類型的數據,
就本例子而言,只“實例化”了一種int類型的接口。因此當我想排序一個float類型的數組時,我就必須在.a文件里面再“實例化”一個float接口。
顯然,假如我想把該具有sort功能的類,抽象成一個獨立的模塊,但是我并不知道該.a的用戶想排序的數據類型是什么,那么將必須窮舉所有的數據類型
這顯然是不可能的。這一局限性不只時模板類,同樣的,模板函數也是如此。
結論:最好不要在靜態鏈接庫中使用模板,同樣的,動態鏈接庫也一樣。
想到這里,腦子里忽然蹦出來一個想法:c++的stl到底是動態鏈接韓式靜態鏈接的呢?stl使用了大量的模板,按照這篇博客在討論的內容,似乎是矛盾的。在網上找了半天
參考知乎的大神們是怎么解釋的吧:
https://www.zhihu.com/question/46098144
以上就是本文的全部心得和內容,如果大家還有所疑問和建議,可以在下面的留言區討論。
原文鏈接:https://www.cnblogs.com/real-madrid/p/7862087.html