Linux靜態(tài)鏈接庫與模板類的處理方式
在閱讀本文之前,小編先給大家介紹一篇相關(guān)文章:Linux靜態(tài)鏈接庫使用類模板的快速排序算法
大家首先看下以上的文章對理解下面的知識點(diǎn)會有很大的幫助。
當(dāng)模板遇到靜態(tài)鏈接庫會發(fā)生什么呢。
我們先按照常規(guī)思路去考慮一個(gè)靜態(tài)鏈接庫的步驟:
1.將某些功能提取出來,放進(jìn)一個(gè)cpp文件,并將接口或者對外導(dǎo)出的類放在頭文件中
2.gcc -c編譯該文件,生成.o
3.ar命令將.o文件打包成.a,即靜態(tài)鏈接庫
4.編譯main函數(shù),并將該靜態(tài)鏈接庫鏈接,生成可執(zhí)行文件。
OK,按照這個(gè)思路,我們將之前寫的快速排序代碼修改后,如下:
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)鏈接庫里面居然沒有這個(gè)myQsortNoRecur函數(shù),可是我明明在快速排序這個(gè)類SORT里面實(shí)現(xiàn)了這個(gè)函數(shù)啊。
用nm命令看下:

實(shí)實(shí)在在的,符號很少,確實(shí)沒有我之前寫的函數(shù)。這就奇怪了,今天下午在網(wǎng)上搜了很久,原來是模板類的原因?qū)е碌模?/p>
因?yàn)樵诰幾g動態(tài)鏈接庫中,我們并沒有指定template class的type,那么靜態(tài)鏈接庫中自然不知道按照什么type去編譯該class中成員函數(shù)。
參考文獻(xiàn):在動態(tài)庫和靜態(tài)庫中使用模板(dynamic libraries ,static libraries)
有沒有解決辦法呢?答案是肯定的,只要我們在靜態(tài)鏈接庫中申明一個(gè)type,并調(diào)用該指定type的函數(shù),那么靜態(tài)鏈接庫中就有函數(shù)原型了。
我覺得可以把該過程稱為接口的“實(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);
}
}
好,重復(fù)上面的編譯過程:

這些編譯和執(zhí)行過程就能正常進(jìn)行了。
但是這種所謂的“實(shí)例化”過程有一個(gè)明顯的缺點(diǎn),那就是,本身這個(gè)SORT類是一個(gè)模板類,可以排序任意類型的數(shù)據(jù),
就本例子而言,只“實(shí)例化”了一種int類型的接口。因此當(dāng)我想排序一個(gè)float類型的數(shù)組時(shí),我就必須在.a文件里面再“實(shí)例化”一個(gè)float接口。
顯然,假如我想把該具有sort功能的類,抽象成一個(gè)獨(dú)立的模塊,但是我并不知道該.a的用戶想排序的數(shù)據(jù)類型是什么,那么將必須窮舉所有的數(shù)據(jù)類型
這顯然是不可能的。這一局限性不只時(shí)模板類,同樣的,模板函數(shù)也是如此。
結(jié)論:最好不要在靜態(tài)鏈接庫中使用模板,同樣的,動態(tài)鏈接庫也一樣。
想到這里,腦子里忽然蹦出來一個(gè)想法:C++的STL到底是動態(tài)鏈接韓式靜態(tài)鏈接的呢?STL使用了大量的模板,按照這篇博客在討論的內(nèi)容,似乎是矛盾的。在網(wǎng)上找了半天
參考知乎的大神們是怎么解釋的吧:
https://www.zhihu.com/question/46098144
以上就是本文的全部心得和內(nèi)容,如果大家還有所疑問和建議,可以在下面的留言區(qū)討論。
相關(guān)文章
Apache下通過shell腳本提交網(wǎng)站404死鏈的方法
這篇文章主要介紹了Apache下通過shell腳本提交網(wǎng)站404死鏈,需要的朋友可以參考下2017-09-09
Linux?配置時(shí)間服務(wù)器的詳細(xì)過程
文章介紹了如何在Linux系統(tǒng)上配置時(shí)間服務(wù)器,包括同步阿里云服務(wù)器時(shí)間和服務(wù)端、客戶端的配置方法,以及在本地服務(wù)器上進(jìn)行時(shí)間同步的步驟,感興趣的朋友一起看看吧2025-03-03
解決Linux常用命令“l(fā)l”失效或命令未找到的問題
這篇文章主要介紹了Linux常用命令“l(fā)l”失效或命令未找到的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Apache 配置多端口 多虛擬主機(jī) 局域網(wǎng)訪問示例
這篇文章主要介紹了Apache如何配置多端口 多虛擬主機(jī) 局域網(wǎng)訪問,需要的朋友可以參考下2014-05-05
CentOS 7下修改默認(rèn)網(wǎng)卡名為eth0的兩種方法
今天又從Centos 6.5裝回了Centos 7,畢竟還是要順應(yīng)潮流嘛。安裝完成之后,發(fā)現(xiàn)發(fā)現(xiàn)CentOS 7默認(rèn)的網(wǎng)卡名稱是eno16777736,所以想著改回eth0,下面這篇文章主要介紹了CentOS 7下修改默認(rèn)網(wǎng)卡名為eth0的兩種方法,需要的朋友可以參考借鑒。2017-02-02
使用Kubeadm在CentOS7.2上部署Kubernetes集群的方法
本篇文章主要介紹了使用Kubeadm在CentOS7.2上部署Kubernetes集群的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
linux系統(tǒng)虛擬主機(jī)開啟支持SourceGuardian(sg11)加密組件的詳細(xì)步驟
這篇文章主要介紹了linux系統(tǒng)虛擬主機(jī)開啟支持SourceGuardian(sg11)加密組件的詳細(xì)步驟,需要的朋友可以參考下2020-12-12

