一、先看看测试程序
1.库部分
hello.h
#ifndef ANDROID_Hello_H
#define ANDROID_Hello_H
class Hello_Base
{
public:
virtual ~Hello_Base() {};
virtual int setctl(int ctl) = 0;
virtual int getctl(int ctl) = 0;
};
class Hello : public virtual Hello_Base
{
public:
virtual ~Hello() {};
static Hello* Instance();
int setctl(int ctl);
int getctl(int ctl);
private:
static Hello* _instance;
int myctl;
};
extern "C" Hello_Base* CreatHello();
typedef Hello_Base* CreatHelloClass();
#endif
hello.cpp
#include "hello.h"
#include <stdio.h>
extern "C" Hello_Base* CreatHello(){
return Hello::Instance();
}
Hello* Hello::_instance = 0;
Hello* Hello::Instance()
{
printf("TK------>>>>>>Hello::Instance---------\n");
if(_instance == 0){
_instance = new Hello();
}
return _instance;
}
int Hello::setctl(int ctl)
{
printf("TK------->>>>>>setctl-----\n");
this->myctl = ctl;
return 1;
}
int Hello::getctl(int ctl)
{
printf("TK------->>>>>>getctl-----\n");
return this->myctl;
}
编译成动态链接库,一般要求PIC位置无关:
g++ -shared -fPIC -o libhello.so hello.cpp
生成:libhello.so
2.测试用例
test.cpp
#include "so/hello.h"
#include <stdio.h>
#include <dlfcn.h>
int main()
{
void* hello = dlopen("/home/lianxi/c++/ku/so/libhello.so", RTLD_LAZY);
if(!hello){
printf("dlopen /home/lianxi/c++/ku/so/libhello.so error!\n");
return -1;
}
dlerror();
CreatHelloClass* Creat_Hello = (CreatHelloClass*)dlsym(hello,"CreatHello");
const char* dlsym_error = dlerror();
if (dlsym_error) {
printf("error %s\n",dlsym_error);
return -1;
}
Hello_Base* mHello = Creat_Hello();
mHello->setctl(5);
int result = mHello->getctl(0);
printf("TK------>>>>>result is %d\n",result);
dlclose(hello);
return 1;
}
编译:g++ -o test test.cpp -L. -ldl
执行./test结果:
TK------>>>>>>Hello::Instance---------
TK------->>>>>>setctl-----
TK------->>>>>>getctl-----
TK------>>>>>result is 5
二、分析
1.libdl库为C语言开发,当需要在C++中dlsym某个符号时、需要声明库中该符号为extern “C”,比如例子中的extern "C" Hello_Base* CreatHello();
2.由于不可能像C语言那样将C++类中的那么多方法都dlsym出来,这里使用了C++中对于纯虚类的实例化是运行时链接的特点;也就是说要在C++中对于类使用共享库的动态链接、必须定义一个纯虚的子类。
关于C中的共享库相关,请看如下链接: