在 C++ 中嵌入脚本引擎
我正在研究如何通过脚本功能最好地扩展 C++ 应用程序,并且正在研究 Python 或 JavaScript。用户定义的脚本需要能够访问应用程序的数据模型。
你们中有人有嵌入这些脚本引擎的经验吗?有哪些潜在的陷阱?
I'm researching how to best extend a C++ application with scripting capability, and I am looking at either Python or JavaScript. User-defined scripts will need the ability to access the application's data model.
Have any of you had experiences with embedding these scripting engines? What are some potential pitfalls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Lua 也是嵌入程序的绝佳选择。它非常独立,甚至原生的跨语言调用系统也不错。
对于 JavaScript,您现在最好的选择是查看 V8(来自 Google),它很容易使用。
Lua is also a great candidate for embedding in programs. Its very self contained, and even the native cross-language call system isn't bad.
For JavaScript, your best bet right now is to look at V8 (from Google), which is easy enough to work with.
使用 Boost: 嵌入 Python 确实很容易Python 库(好吧,好吧,讽刺。)当涉及到跨语言功能时,没有什么是“容易的”。 Boost 在帮助此类开发方面做了很多工作。与我共事的一位开发人员对 Boost->Python 界面发誓。用户可以使用 Python 对其代码进行编程,并在 UI 中内置 REPL。惊人的。
然而,使用 SWIG 和其他语言(例如 Java)可以更好地观察我的经验。我目前正在与 SWIG 合作,用 Python 包装 C++。存在各种异常、线程、跨语言多态性等问题。
我会先看看这两个地方。正如我所说,没有什么是“容易的”,但这两者都让生活变得更加宜居。
It's sure easy to embed Python by using the Boost::Python library (ok, ok, sarcasm.) Nothing is "easy" when it comes to cross-language functionality. Boost has done a great deal to aid such development. One of the developers I've worked with swears on the Boost->Python interface. His code can be programmed by a user in Python, with a REPL built right into the UI. Amazing.
However, my experience has been better observed using SWIG and other languages such as Java. I'm currently working with SWIG to wrap C++ with Python. There's all sorts of gotchas with exceptions, threading, cross-language polymorphism and the like.
I'd look at these two places first. As I said, nothing will be "easy" but both these make life more livable.
除非你真的喜欢 Python 或 Javascript,否则我会考虑使用 Lua。由于它完全被设计为嵌入式脚本引擎,因此它消除了与 C 和 C++ 已经做得很好的部分的重叠。只要您仅使用 C 可调用函数在代码和 Lua 引擎之间进行接口,嵌入起来也相当容易。
如果你想使用 C++ 级别的接口,你可能需要看看 LuaBind,它允许从您编写的 C++ 类(它生成的代理)派生 Lua 类之类的东西。
Unless you're really set on Python or Javascript, I'd give some consideration to using Lua. Since it's designed entirely as an embedded scripting engine, it eliminates quite a bit of overlap with what C and C++ already do well. It's also pretty easy to embed as long as you only interface between your code and the Lua engine in terms of C callable functions.
If you want to use a C++ level interface, you might want to take a look at LuaBind, which allows things like a Lua class deriving from (the proxy it generates for) a C++ class you wrote.
看看天使脚本
简单且易于嵌入,类似 c/c++ 的语法。免费且跨平台。你可以在几个小时内开始。
Have a look at angelscript
simple and easy to embed, c/c++ like syntax. free and corss-platform. u can get start in a few hrs.
Boost::Python,正如wheaties 的回答一样,是一个非常成熟的解决方案。
Lua 因易于嵌入而闻名,但我自己还没有尝试过。
作为 R 的用户,我对嵌入 R 可以使用 RInside 包。一个简单的例子是
,包中还有几个例子。 RInside 本质上使用一些 Rcpp接口包。
Boost::Python, as in wheaties answer, is a very mature solution.
Lua has a reputation for being easy to embed but I have not tried this myself.
As a user of R, I am more interested in embedding R which is possible using the RInside package. A simple example is
and there are a couple more examples in the package. RInside essentially provides you a nice wrapper around the R engine using some of the Rcpp interface package.