日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

用C++面向对象的方式动态加载so

發(fā)布時(shí)間:2024/4/11 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用C++面向对象的方式动态加载so 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這幾天在寫一個(gè)server,由于框架相同,僅僅是獲取數(shù)據(jù)源的地方有區(qū)別,所以,研究了一下如何使用面向?qū)ο蟮姆椒▉韯?dòng)態(tài)加載so。

主要思想就是:
1.通過一個(gè)函數(shù)能夠獲得一個(gè)基類的指針,這樣在調(diào)用基類的函數(shù)的時(shí)候,就能自動(dòng)調(diào)用子類的實(shí)現(xiàn)了。
2.存儲(chǔ)so對象的指針應(yīng)該是外層類的一個(gè)static變量。

詳細(xì)還是看代碼吧:
1)首先定義一個(gè)公共的頭文件,里面存儲(chǔ)的基類的定義:(需要注意的就是,只要不是純虛函數(shù),那么就一定要有實(shí)現(xiàn);還有就是析構(gòu)函數(shù)需要為虛函數(shù))
so和主調(diào)程序都需要包含這個(gè)頭文件。

source_base.h

#ifndef _SOURCE_BASE_H_ #define _SOURCE_BASE_H_ #include <iostream> using namespace std; class CSourceBase; /** * @brief 獲取實(shí)例 * * @param client new出的指針 * * @return 0 succ * else fail */ extern “C” int CreatObj(CSourceBase *& client); class CSourceBase { public: CSourceBase(){}; virtual ~CSourceBase(){}; public: virtual int GetFriends(int iUin,char *data,int &iLen,int maxLen) { return 0; } }; #endif

2)我們來看so的實(shí)現(xiàn)

xy_source.h

#ifndef _XY_SOURCE_H_ #define _XY_SOURCE_H_ #include <iostream> #include “sourcebase.h” using namespace std; class CXY_Source:public CSourceBase { public: CXY_Source(); virtual ~CXY_Source(); {} int GetFriends(int iUin,char *data,int &iLen,int maxLen); }; #endif

xy_source.cpp

#include “xy_source.h” int CreatObj(CSourceBase *& client) { client = new CXY_Source(); return 0; } CXY_Source::CXY_Source() { } CXY_Source::~CXY_Source() { } int CXY_Source::GetFriends(int iUin,char *data,int &iLen,int maxLen) { return 0; }

3)調(diào)用者的實(shí)現(xiàn)

這里說明一下,因?yàn)檫@里想要對外封裝成透明的,所以,采用了如下的方式。
xy_client.h

#ifndef _XY_CLIENT_H_ #define _XY_CLIENT_H_ #include <iostream> #include “sourcebase.h” using namespace std; typedef int (*FunPtr)(CSourceBase *& client); class CXY_Client { public: static void *SoObj; public: CXY_Client(); virtual ~CXY_Client(); //載入so int Init(const char * soname); int GetFriends(int iUin,char *data,int &iLen,int maxLen); private: CSourceBase *m_Client; };

xy_client.cpp

#include “xy_client.h” void* CXY_Client::SoObj=NULL; CXY_Client::CXY_Client() { m_Client = NULL; } CXY_Client::~CXY_Client() { if(m_Client) { delete m_Client; } } int CXY_Client::Init(const char * soname) { string strSoName; if(soname==NULL) { strSoName = “../lib/xysource.so”; } else { strSoName = soname; } if(strSoName.size()==0) { strSoName = “../lib/xysource.so”; } if(CXY_Client::SoObj == NULL) { SoObj=dlopen((char*)strSoName.c_str(),RTLD_LAZY); if(SoObj==NULL) { return -1; } } if(m_Client==NULL) { FunPtr func; func = (FunPtr)dlsym(SoObj, “CreatObj”); int ret = (*func)(m_Client); if(ret) { return -2; } } return 0; } int CXY_Client::GetFriends(int iUin,char *data,int &iLen,int maxLen) { return m_Client->GetFriends(iUin,data,iLen,maxLen); }

OK,到此為止代碼就結(jié)束了,大家可能會(huì)發(fā)現(xiàn)我沒有調(diào)用dlclose,這是因?yàn)閟tatic變量沒有必要來調(diào)用,在進(jìn)程結(jié)束時(shí)會(huì)自動(dòng)釋放句柄,當(dāng)然如果需要有釋放的應(yīng)用場景的話,可以通過增加計(jì)數(shù)來實(shí)現(xiàn)。

另外由于上面的這個(gè)實(shí)例是從項(xiàng)目中摳出來的,所以并不能直接編譯,還望大家見諒。
但是在這里可以下載到一個(gè)簡單的可編譯實(shí)例,可以用來作為實(shí)現(xiàn)so動(dòng)態(tài)加載編程的第一步~~

附:dlopen參數(shù)說明:

void *dlopen(const char *filename, int flag); 其中flag有:RTLD_LAZY RTLD_NOW RTLD_GLOBAL,其含義分別為: RTLD_LAZY:在dlopen返回前,對于動(dòng)態(tài)庫中存在的未定義的變量(如外部變量extern,也可以是函數(shù))不執(zhí)行解析,就是不解析這個(gè)變量的地址。 RTLD_NOW:與上面不同,他需要在dlopen返回前,解析出每個(gè)未定義變量的地址,如果解析不出來,在dlopen會(huì)返回NULL,錯(cuò)誤為: : undefined symbol: xxxx……. RTLD_GLOBAL:它的含義是使得庫中的解析的定義變量在隨后的隨后其它的鏈接庫中變得可以使用。

總結(jié)

以上是生活随笔為你收集整理的用C++面向对象的方式动态加载so的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。