基类为抽象类,在不同的动态库中实现不同的执行行为,但是每个动态库要提供2个统一的方法:1) baseClass * create(); 2) void destroy( baseClass* );,调用该实际类的上下文,通过dlopen,dlsym( dl, "create"), dlsym( dl, "destroy")来获得实际对象的句柄。实际上是一种工厂/builder模型。
1. 基类
//base.h
#include <iostream> class baseClass { public: virtual void test(){}; virtual ~baseClass(){}; }; //typedef baseClass* create_t(); typedef baseClass* (*create_t)(); typedef void (*destroy_t)(baseClass*);
2. 继承类
#include "base.h" #include <iostream> class a: public baseClass { public: void test() { std::cout << "in a" << std::endl; } }; // the class factories extern "C" baseClass* create() { return new a; } extern "C" void destroy(baseClass* p) { delete p; }
编译方法:
> g++ -g -fPIC a.cpp
> g++ -g -shared -o liba.so a.o
3. 调用类
#include "base.h"
void *load_so(const char*so_path) { void* dl = dlopen(so_path, RTLD_LAZY); if (!dl) { cerr << "Cannot load library: " << dlerror() << '\n'; } return dl; } int test_so(void *dl) { if (!dl) { return 0; } create_t create_func = (create_t) dlsym(dl, "create"); destroy_t destroy_func = (destroy_t) dlsym(dl, "destroy"); if (!create_func || !destroy_func) { cerr << "Cannot load symbols: " << dlerror() << '\n'; return -1; } baseClass* base = create_func(); base->test(); destroy_func(base); dlclose(dl); return 0; }
int main()
{
void *dl = load_so("./liba.so");
test_so(dl);
dl = load_so("./libb.so");
test_so(dl);
dl = load_so("./libc.so");
test_so(dl);
return 0;
}