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();
} create_t * create_triangle = (create_t *)dlsym(handle, "create"); CPolygon * pObj = create_triangle(); if( != pObj )
{
pObj->area();
} delete pObj; dlclose(handle);
return ;
}

生成动态库: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

/***************************************************************************/
/* dynclass.h */
/* 使用的时候#include "dynclass.h",在类的声明后使用DYNCLASS(类名)注册*/
/***************************************************************************/ #ifndef __DYNAMIC_H__
#define __DYNAMIC_H__ #include <cstdio>
#include <string>
#include <typeinfo>
#include <cstring> #if !defined ( DYN_DECLARE )
#define DYN_DECLARE(class_name) DYN_CLASS::CFactory<class_name> class_name
#endif #if !defined ( DYN_CREATE )
#define DYN_CREATE(class_name) DYN_CLASS::Create(class_name)
#endif namespace DYN_CLASS
{
/* create object by class name */
void * Create( const char * class_name ); /* interface of class factory*/
class CAbstractFactory
{
public:
virtual void * Create( const char * class_name ) = ;
}; /* list of class factory */
class CFactoryList
{
friend void * Create( const char * class_name );
private:
static CFactoryList * _head;
CFactoryList * m_next;
CAbstractFactory * m_item;
public:
CFactoryList( CAbstractFactory * fact );
virtual ~CFactoryList( void );
}; /* ctor of CFactoryList, add a class factory to list */
inline CFactoryList::CFactoryList( CAbstractFactory * fact )
: m_item( fact )
{
m_next = _head;
_head = this;
} #if defined ( _MSC_VER )
/* disable warning for the following line : CFactory( void ): m_item( this ) {} */
#pragma warning(disable : 4355)
#endif /* realization of class factory */
template <class t>
class CFactory: public CAbstractFactory
{
static t _object;
CFactoryList m_item; public: /* add itself to list of class factory when constructed */
CFactory( void ) : m_item( this ) {} virtual ~CFactory() {} /* create object of this class if matched */
void * Create( const char * class_name )
{
std::string strClassName; #if defined (_MSC_VER )
strClassName = ( "class " );
#else
char szSize[] = {};
sprintf(szSize, "%d", strlen(class_name) );
strClassName = szSize;
#endif
strClassName += class_name;
printf("%s vs %s \n", typeid(_object).name(), strClassName.c_str());
/* RTTI support */
return !strcmp( typeid(_object).name(), strClassName.c_str() )
? (void*)( new t ) : ; }
};
}
#endif /* __DYNAMIC_H__ */
/***************************************************************************/
/* dynclass.cpp */
/***************************************************************************/
#include "dynclass.h" namespace DYN_CLASS
{
CFactoryList * CFactoryList::_head = ; void *Create( const char * class_name )
{
void * new_object = ;
const CFactoryList * cur = CFactoryList::_head; for( ; cur ; cur = cur->m_next )
{
printf("find \n");
/* if class_name matched, object will then be created and returned */
if( new_object = cur->m_item->Create(class_name) )
{
break;
}
} return new_object;
} /* delete linkage from CFactoryList when some class factory destroyed */
CFactoryList::~CFactoryList( void )
{
CFactoryList ** m_nextp = &CFactoryList::_head; for( ; *m_nextp ; m_nextp = &(*m_nextp)->m_next )
if( *m_nextp == this )
{
*m_nextp = (*m_nextp)->m_next;
break;
}
}
}

参考:

  1. Why does C++ not have reflection?
  2. How can I add reflection to a C++ application?
  3. https://my.oschina.net/u/1450061/blog/204563
  4. https://my.oschina.net/u/1450061/blog/204608

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

  6. http://m.blog.csdn.net/article/details?id=9254239

  7. http://blog.csdn.net/dengyunze/article/details/763558

  8. http://www.ithao123.cn/content-9688046.html

C语言如何开发简单的插件的更多相关文章

  1. 用GO语言开发editplus编辑器插件(附源码)

    我要开发的插件功能极为简单,就是对用户选中的内容进行base64编码或解密工作. 其中所涉及的技术部分主要是GO语言程序开发和editplus插件配置的部分,首先我们来看一下GO语言代码的写法,如下: ...

  2. [Songqw.Net 基础]WPF实现简单的插件化开发

    原文:[Songqw.Net 基础]WPF实现简单的插件化开发 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/ar ...

  3. JVM 平台上的各种语言的开发指南

    JVM 平台上的各种语言的开发指南 为什么我们需要如此多的JVM语言? 在2013年你可以有50中JVM语言的选择来用于你的下一个项目.尽管你可以说出一大打的名字,你会准备为你的下一个项目选择一种新的 ...

  4. Sublime Text 3前端开发常用优秀插件介绍

    . 首页 博客园 联系我 前言:关于Sublime Text 3. Package Control插件管理. Package Control使用方法/安装Emmet插件. Emmet插件. JsFor ...

  5. 在NPAPI开发火狐浏览器插件在NPAPI插件

    1.插件是什么 插件是一种遵循一定规范的应用程序接口编写出来的程序.插件必须依附于一个宿主程序,为宿主程序提供增强功能.插件的种类有很多,这里主要讨论浏览器插件. IE下利用OLE和COM技术开发的浏 ...

  6. VS简单注释插件——VS插件开发续

    VS简单注释插件——VS插件开发续 前些时候,我写过一篇<VS版权信息插件——初试VS插件开发小记>分享过一个用于添加注释信息的插件,但那个插件有几个问题: 不能添加带块注释(/**/), ...

  7. 如何开发一个 PyCharm 插件

    PyCharm 是很多 Python 开发者优先选择的 IDE,功能强大,跨平台,提供免费社区版,非常良心.如果你想自己给PyCharm添加一些功能怎么办呢?有两个办法: 通过提需求实现,到 JetB ...

  8. C语言可以开发哪些项目?

    C语言是我们大多数人的编程入门语言,对其也再熟悉不过了,不过很多初学者在学习的过程中难免会出现迷茫,比如:不知道C语言可以开发哪些项目,可以应用在哪些实际的开发中--,这些迷茫也导致了我们在学习的过程 ...

  9. 如何开发一个Jquery插件

    Jquery有两种开发插件的方法: 1.jquery.fn.extend(object); 2.jquery.extend(object); 第一种方法是给Jquery对象添加方法,jquery.fn ...

随机推荐

  1. 【MongoDB】 基于C#官方驱动2.2版的封装类

    一.前言 最近项目中要用到MongoDB,因此实现做了不少的调研.发现网上很多现有关于MongoDB C#官方驱动的调用方法都是基于1.8版本的,已经不是用了最新的2.2版本.因此我在基于C#官方驱动 ...

  2. 联合体(union)的使用方法及其本质

    转自:http://blog.csdn.net/huqinwei987/article/details/23597091 有些基础知识快淡忘了,所以有必要复习一遍,在不借助课本死知识的前提下做些推理判 ...

  3. hdu 5071(2014鞍山现场赛B题,大模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...

  4. Hibernate的一级缓存

    Hibernate的一级缓存 什么是缓存:缓存将数据库/硬盘上文件中数据,放入到缓存中(就是内存中一块空间).当再次使用的使用,可以直接从内存中获取 缓存的好处:提升程序运行的效率.缓存技术是Hibe ...

  5. python 异常

    引用一段来自菜鸟教程的文章:http://www.runoob.com/python/python-exceptions.html Python 异常处理 python提供了两个非常重要的功能来处理p ...

  6. Python爬虫学习(1): urllib的使用

    1.urllib.urlopen 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作 In [1]: import urllibIn [2]: file = urllib.urlo ...

  7. CodeForces445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. Linux学习总结

    1.软链接和硬链接 ln 命令可用来创建硬链接或是符号链接.它的使用方式有两种. ln file link 用来创建硬链接 ln -s item link 用来创建符号链接,这里的item可以是文件也 ...

  9. 从零开始山寨Caffe·肆:线程系统

    不精通多线程优化的程序员,不是好程序员,连码农都不是. ——并行计算时代掌握多线程的重要性 线程与操作系统 用户线程与内核线程 广义上线程分为用户线程和内核线程. 前者已经绝迹,它一般只存在于早期不支 ...

  10. bzoj2141排队(辣鸡但是好写的方法)

    题意很明确,也非常经典: 一个支持查询 区间中比k大的数的个数 并且支持单点修改的序列 ——因为题意可以转化为:查询这两个数中比后者大的个数.比后者小的个数.比前者大的个数.比前者小的个数(根据这4个 ...