扩展 C++具有嵌入式脚本的应用程序
我正在开发一个 C++ 应用程序,需要兼容多个平台(Windows/Linux),并希望授予用户扩展软件以完全满足他们的需求的能力,而不允许他们更改应用程序的关键部分(所以我不希望它们在 C++ 代码中)。
我正在寻找的是嵌入一种脚本语言(我更喜欢Python,因为我已经熟悉它,但这不是强制性的),因此如果我希望这些对象可修改的。
最简单的例子:如果有人想为我的应用程序构建自己的 UI,他们应该能够使用这样的脚本来实现。
然而问题是,我从未将 C++ 和任何类型的外部脚本放在一起,所以我真的不知道如何开始。在寻找入门材料后,我发现 Lua 声称是一种很好的语言,但我找不到好的初学者教程。
如果有人知道一个好的起点,无论是在线资源还是一本好书,我将非常感激。我不介意花几块钱买一本好书。
作为一名学习者,我倾向于从示例代码和几行解释代码的组合中学到最好的东西。
I am developing a C++ application that needs to be multiple platform compatible (Windows/Linux) and want to grant users the ability to extend the software to fit their needs exactly without allowing them to change critical parts of the application (so I do not want them in the C++ code).
What I am looking for is to embed a scripting language (I would prefer Python because I am familiar with it already, but it's not mandatory), so scripts put in some plugin folder can manipulate objects of the application if I want these objects to be modifyable.
The most simple example: if someone wants to build their own UI for my application, they should be able to do so with such a script.
The problem however is, that I've never put C++ and any kind of external scripts together, so I really do not know how to start. After looking for material to get started, I found that Lua claims to be a good language to do that, but I could not find good beginner tutorials.
I would really appreciate if someone knew a good place to start, be it online resources, or a good book. I wouldn't mind spending a few bucks on a good book.
As a learner, I tend to learn best from a mix of example code and a few lines explaining those.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议你阅读Programming in Lua,这本书有一整节介绍如何嵌入Lua在 C(和 C++)中。
亚马逊用户。
该语言还有相当不错的在线文档和活跃的邮件列表。
I would suggest you to read Programming in Lua, this book has an entire section on how to embed Lua in C (and C++).
It is very highly rated by Amazon users.
The language has also pretty good online documentation and a active mailing list.
如果你想使用Python,我绝对建议使用 Boost.Python。这是一个设计精良的图书馆。举个例子:要向 Python 公开 C++ 类,您所要做的就是:
它自动处理几乎所有事情:类型之间的转换、异常,它甚至允许您通过 boost 在两种语言之间使用引用计数对象: :shared_ptr。
If you want to use Python I would definitely suggest using Boost.Python. It is an incredibly well designed library. Just an example: all you have to do to expose a C++ class to Python is this:
It handles almost everything automatically: conversions between types, exceptions, it even allows you to use reference counted objects between the two languages with
boost::shared_ptr
.Linux Journal 上的文章是一个很好的起点,可以帮助您了解如何在您的计算机中嵌入 Python 解释器。 c/c++ 代码。然而,这只是成功的一半,因为嵌入解释器后,您需要将软件的某些部分发布到脚本环境。执行此操作的基本 API 是用 C 编写的,如果您的大部分代码是 C++,那么最好使用 boost::python,因为围绕 C++ 类编写 C 包装器可能很麻烦。您还可以使用 Py++ 生成 boost::python 绑定。
如果您只想使用脚本作为定制的大门,并且可以忍受 python 的内存占用,那么它可能是比 Lua 更好的选择。 Lua 通常适用于游戏开发等小型环境。 Python 开发人员也比 lua 开发人员多得多,并且可用的内置函数和第三方库也更多。
The article here at linux journal is a good place to start on how to embed a python interpreter in your c/c++ code. This is only half the battle however because when the interpreter is embedded, you then need to publish some part of your software to the scripting environment. The basic API to do so is in C and if most of your code is C++, it might be best to use boost::python since writing C wrappers around your C++ classes might be cumbersome. You can also use Py++ to generate the boost::python binding.
If you only want to use scripting as a door to customization and you can live with the memory footprint of python, it might be a better choice than Lua. Lua is usually good for small environment like game development. There is also a lot more python developers than lua developers as well as more built-ins and third party libraries available.
对于Python,我猜boost库就是为了做到这一点。至于 Lua,我自己还没有使用过,但是快速的 Google 搜索首先让我找到了 debian admin 然后到Lua的C接口。你调查过那些吗?
for Python, I guess the boost library is meant to do it. As for Lua, I haven't used it myself, but a quick Google search first led me to debian admin and then to Lua's C interface. Have you looked into those?