首页 > 代码库 > C语言如何开发简单的插件

C语言如何开发简单的插件

linux 通过dlopen来实现:

#include "polygon.h"#include <stdlib.h>#include <dlfcn.h>int main(){    typedef CPolygon* create_t();    void * handle = dlopen("./triangle.so", RTLD_LAZY);    if( !handle )    {    std::cerr << dlerror() << std::endl;    exit(1);    }    create_t * create_triangle = (create_t *)dlsym(handle, "create");    CPolygon * pObj = create_triangle();    if( 0 != pObj )    {    pObj->area();    }    delete pObj;    dlclose(handle);    return 0;}

生成动态库:g++ -fPIC -shared triangle.cpp -o triangle.so

生成test主函数:g++ -fPIC test.cpp -ldl -o test

具体demo:http://files.cnblogs.com/files/hero4china/plugins_1.zip

 

参考:

https://my.oschina.net/u/1450061/blog/204564

https://my.oschina.net/u/1450061/blog/204608

https://my.oschina.net/u/1450061/blog/204563 

C语言如何开发简单的插件