简体   繁体   中英

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).

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.

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 .

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:

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 .

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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