使用C中的libdl插件架构 [英] Plugin architecture in C using libdl

查看:118
本文介绍了使用C中的libdl插件架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩弄左右,C语言编写的小IRC框架,我现在要去一些核心功能的拓展 - 但除此之外,我想它是可扩展的插件与

I've been toying around, writing a small IRC framework in C that I'm now going to expand with some core functionality - but beyond that, I'd like it to be extensible with plugins!

直到现在,每当我写的东西IRC相关的(我写了很多,现在在约6种不同的语言......我着火了!),居然径自实现插件架构,这是内部的除$ p $是有设施做(读:滥用)PTED语言。所以,像干扰整个脚本文件低谷评估的红宝石(!坏)

Up until now, whenever I wrote something IRC related (and I wrote a lot, in about 6 different languages now... I'm on fire!) and actually went ahead to implement a plugin architecture, it was inside an interpreted language that had facilities for doing (read: abusing) so, like jamming a whole script file trough eval in Ruby (bad!).

现在我想在C到滥用的东西!

Now I want to abuse something in C!

基本上有三件事情我可以做的。

Basically there's three things I could do


  1. 定义我的节目里面一个简单的脚本语言

  2. 使用现有的,嵌入一个跨preTER

  3. 使用libdl加载*。所以在运行时模块

我喜欢第三个和raather避免其他两个选择,如果可能的。也许我是某种形式的受虐狂,但我认为这可能是既有趣又有益的学习目的。

I'm fond of the third one and raather avoid the other two options if possible. Maybe I'm a masochist of some sort, but I think it could be both fun and useful for learning purposes.

在逻辑思维,明显的痛链将是(从最低到最高)2 - > 1 - > 3,对于libdl正在处理原始code,可以(并且)爆炸的原因很简单,我的脸往往不是。

Logically thinking, the obvious "pain-chain" would be (lowest to highest) 2 -> 1 -> 3, for the simple reason that libdl is dealing with raw code that can (and will) explode in my face more often than not.

所以,这个问题超出你,计算器的​​其他用户,你认为libdl达到这个任务,甚至是一个现实的想法?

So this question goes out to you, fellow users of stackoverflow, do you think libdl is up to this task, or even a realistic thought?

推荐答案

libdl 是非常适合插件架构 -​​ 一定范围内:-)。它使用了很多关于正是这种目的在许多不同的软件。它工作得很好的情况下,其中有主程序和插件之间的良好定义的API /接口,以及许多不同的插件实现相同的API /接口。例如,您的IRC客户端可能有插件实现网关到不同的IM协议(Jabber的,MSN的Sametime等) - 所有这些都是非常相似的,所以你可以使用的功能,如发送消息定义一个API ,检查的答复等等 - 并写一堆插件,每个实施方案中的不同

libdl is very well suited to plug-in architectures - within certain boundaries :-). It is used a lot for exactly this sort of purpose in a lot of different software. It works well in situations where there is a well-defined API/interface between the main program and the plug-in, and a number of different plug-ins implement the same API/interface. For instance, your IRC client might have plug-ins that implement gateways to different IM protocols (Jabber, MSN, Sametime etc...) - all of these are very similar, so you could define an API with functions such as "send message", "check for reply" etc - and write a bunch of plug-ins that each implemented a different one of the protocols.

在那里工作较差的是你想拥有插件使主程序行为的任意改变形势 - 的方式中,例如,Firefox插件可以改变浏览器选项卡的行为,其外观,添加/删除按钮,等等。这种事情是很容易在动态语言来实现(所以为什么很多的Firefox在JavaScript实现),如果这是你想要的,你可能是您选择最好的那种定制(2),写很多UI的脚本语言...

The situation where it works less well is where you want to have the plug-ins make arbitrary changes to the behaviour of the main program - in the way that, for instance, Firefox plug-ins can change the behaviour of browser tabs, their appearance, add/remove buttons, and so on. This sort of thing is much easier to achieve in a dynamic language (hence why much of Firefox is implemented in javascript), and if this is the sort of customisation you want you may be better off with your option (2), and writing a lot of your UI in the scripting language...

这篇关于使用C中的libdl插件架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆