简体   繁体   English

使用嵌入式脚本扩展C ++应用程序

[英]Extending a C++ application with embedded scripting

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). 我正在开发一个需要多平台兼容的C ++应用程序(Windows / Linux),并希望授予用户扩展软件以完全满足其需求的能力,而不允许他们更改应用程序的关键部分(所以我不想要他们在C ++代码中)。

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. 我正在寻找的是嵌入一种脚本语言(我更喜欢Python因为我已经熟悉它,但它不是强制性的),所以如果我想要这些对象,那么放在一些插件文件夹中的脚本可以操作应用程序的对象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. 最简单的例子:如果有人想为我的应用程序构建自己的UI,他们应该可以使用这样的脚本来完成。

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. 但问题是,我从来没有把C ++和任何类型的外部脚本放在一起,所以我真的不知道如何开始。 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. 在寻找材料开始之后,我发现Lua声称这是一个很好的语言,但我找不到好的初学者教程。

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. 作为一个学习者,我倾向于通过混合的示例代码和解释这些代码的几行来学习。

I would suggest you to read Programming in Lua , this book has an entire section on how to embed Lua in C (and C++). 我建议你阅读Lua编程 ,这本书有关于如何在C(和C ++)中嵌入Lua的整个部分。

It is very highly rated by Amazon users . 它受到亚马逊用户的高度评价。

The language has also pretty good online documentation and a active mailing list . 该语言还有很好的在线文档和活跃的邮件列表

If you want to use Python I would definitely suggest using Boost.Python . 如果你想使用Python,我肯定建议使用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: 举个例子:要将C ++类公开给Python,你需要做的就是:

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
}

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 . 它几乎可以自动处理所有内容:类型之间的转换,异常,它甚至允许您使用boost::shared_ptr在两种语言之间使用引用计数对象。

The article here at linux journal is a good place to start on how to embed a python interpreter in your c/c++ code. linux日志中的文章是开始如何在c / c ++代码中嵌入python解释器的好地方。 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. 这样做的基本API是在C中,如果你的大部分代码都是C ++,那么最好使用boost :: python,因为在你的C ++类中编写C包装可能很麻烦。 You can also use Py++ to generate the boost::python binding. 您还可以使用Py ++生成boost :: python绑定。

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. 如果您只想使用脚本作为自定义的大门,并且您可以使用python的内存占用,那么它可能是比Lua更好的选择。 Lua is usually good for small environment like game development. Lua通常适合游戏开发等小环境。 There is also a lot more python developers than lua developers as well as more built-ins and third party libraries available. 除了lua开发人员之外,还有更多的python开发人员以及更多的内置函数和第三方库。

for Python, I guess the boost library is meant to do it. 对于Python,我想升级库是为了做到这一点。 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 . 至于Lua,我自己并没有使用它,但是快速谷歌搜索首先让我进入debian管理员 ,然后是Lua的C界面 Have you looked into those? 你看过那些吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM