動態(tài)庫與靜態(tài)庫--類篇

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)建

  1. 編譯源文件
g++ -c -o test.o test.cpp
  1. 生成靜態(tài)庫
ar -rcs libtest.a test.o

1.2 使用

  1. 鏈接靜態(tài)庫
g++ -o main main.cpp -L. -ltest

或者

g++ -o main main.cpp ./libtest.a

注意:庫一定要放在命令行的末尾

  1. 測試
./main
  1. 結(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)建

  1. 編譯目標(biāo)文件
g++ -c -fPIC test.cpp -o test.o
  1. 生成動態(tài)庫
g++ -shared test.o -o libtest.so

可以合并為

g++ -shared -fPIC -o libtest.so test.cpp

2.2 使用

  1. 生成可執(zhí)行文件
g++ -o main main.cpp -L. -ltest

或者

g++ -o main main.cpp ./libtest.so

注意:庫一定要放在命令行的末尾

  1. 測試
    指定動態(tài)鏈接庫位置
export LD_LIBRARY_PATH=動態(tài)鏈接庫位置

執(zhí)行

./main
  1. 結(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)步驟如下:

  1. 定義一個抽象類,提供純虛函數(shù)接口。
  2. 具體實現(xiàn)類繼承抽象類。
  3. 提供抽象類對象的創(chuàng)建和銷毀的接口。

3.1 創(chuàng)建

  1. 修改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
}
  1. 修改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;
    }
}
  1. 修改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;
}
  1. 編譯動態(tài)加載庫
g++ -shared -fPIC -o libtest.so test.cpp

3.2 使用

  1. 編譯主程序
g++ -o main -ldl main.cpp
  1. 測試
./main
  1. 結(jié)果
Test()
Func(100)
~Test()

4. 總結(jié)

靜態(tài)庫、共享庫與動態(tài)庫編譯鏈接使用比較
靜態(tài)庫與動態(tài)庫Make file比較
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容