0.前提(文件內(nèi)容)
test.h
#pragma once
class Test{
public:
Test();
~Test();
void Func(int i);
};
test.cpp
#include <iostream>
#include "test.h"
using namespace std;
Test::Test(){
cout << __func__ << "()" << endl;
}
Test::~Test(){
cout << __func__ << "()" << endl;
}
void Test::Func(int i){
cout << __func__ << "(" << i << ")" << endl;
}
main.cpp
#include "test.h"
int main(){
Test t;
t.Func(100);
}
1. 靜態(tài)庫的制作與使用
1.1 創(chuàng)建
- 編譯源文件
g++ -c -o test.o test.cpp
- 生成靜態(tài)庫
ar -rcs libtest.a test.o
1.2 使用
- 鏈接靜態(tài)庫
g++ -o main main.cpp -L. -ltest
或者
g++ -o main main.cpp ./libtest.a
注意:庫一定要放在命令行的末尾
- 測試
./main
- 結(jié)果
Test()
Func(100)
~Test()
嘗試把
test.cpp修改如下,重現(xiàn)編譯libtest.a,再次執(zhí)行./main查看結(jié)果
void Test::Func(int i){
cout << __func__ << "(" << i*100 << ")" << endl;
}
2. 共享庫的制作
2.1 創(chuàng)建
- 編譯目標(biāo)文件
g++ -c -fPIC test.cpp -o test.o
- 生成動態(tài)庫
g++ -shared test.o -o libtest.so
可以合并為
g++ -shared -fPIC -o libtest.so test.cpp
2.2 使用
- 生成可執(zhí)行文件
g++ -o main main.cpp -L. -ltest
或者
g++ -o main main.cpp ./libtest.so
注意:庫一定要放在命令行的末尾
- 測試
指定動態(tài)鏈接庫位置
export LD_LIBRARY_PATH=動態(tài)鏈接庫位置
執(zhí)行
./main
- 結(jié)果
Test()
Func(100)
~Test()
嘗試把
test.cpp修改如下,重現(xiàn)編譯libtest.a,再次執(zhí)行./main查看結(jié)果
void Test::Func(int i){
cout << __func__ << "(" << i*100 << ")" << endl;
}
3. 動態(tài)鏈接庫的制作
類的動態(tài)鏈接庫是利用多態(tài)性動態(tài)加載類,只能用于C++調(diào)用,不能用于C。
具體實現(xiàn)步驟如下:
- 定義一個抽象類,提供純虛函數(shù)接口。
- 具體實現(xiàn)類繼承抽象類。
- 提供抽象類對象的創(chuàng)建和銷毀的接口。
3.1 創(chuàng)建
- 修改
test.h
#pragma once
class ITest{
public:
virtual void Func(int i) = 0;
virtual ~ITest(){}
};
class Test : public ITest{
public:
Test();
~Test();
void Func(int i);
};
extern "C" {
ITest* create(void);
void destroy(ITest *p);
typedef ITest* create_t(void); // create factory
typedef void destory_t(ITest*); // destory
}
- 修改
test.cpp
#include <iostream>
#include "test.h"
using namespace std;
Test::Test(){
cout << __func__ << "()" << endl;
}
Test::~Test(){
cout << __func__ << "()" << endl;
}
void Test::Func(int i){
cout << __func__ << "(" << i << ")" << endl;
}
ITest* create(void){
return new Test;
}
void destroy(ITest *p){
if(NULL != p){
delete p;
}
}
- 修改
main.cpp
#include <iostream>
#include <cstdlib>
#include <dlfcn.h>
#include "test.h"
using namespace std;
int main(){
void *so_handle = dlopen("./libtest.so", RTLD_LAZY);
if (!so_handle) {
cerr << "Error: load so failed." << endl;
exit(-1);
}
create_t *create = (create_t*) dlsym(so_handle, "create");
char *err = dlerror();
if (NULL != err) {
cerr << err;
exit(-1);
}
ITest *pt = create();
pt->Func(100);
destory_t *destroy = (destory_t*) dlsym(so_handle, "destroy");
err = dlerror();
if (NULL != err) {
cerr << err;
exit(-1);
}
destroy(pt);
pt = NULL;
dlclose(so_handle);
return 0;
}
- 編譯動態(tài)加載庫
g++ -shared -fPIC -o libtest.so test.cpp
3.2 使用
- 編譯主程序
g++ -o main -ldl main.cpp
- 測試
./main
- 結(jié)果
Test()
Func(100)
~Test()
4. 總結(jié)

靜態(tài)庫、共享庫與動態(tài)庫編譯鏈接使用比較

靜態(tài)庫與動態(tài)庫Make file比較