C++ 脚本语言

2022-01-04 00:00:00 scripting c++ scripting-language

我对脚本语言有点生疏了,前提是它们最近像蘑菇一样流行:)

I'm getting a little rusty in scripting languages, provided they're popping like mushrooms lately :)

今天我想如果有一种脚本语言可以与 C++ 无缝对话就好了,也就是说,可以使用 C++ 类,而且,对我来说最重要的是,可以编译成 C++ 或某些 DLL/.SO(加上它的 .h),这样我就可以将它链接到我的 C++ 程序中,并使用脚本定义或实现的类.

Today I thought that it would be nice to have a scripting language that talks seamlessly to C++, that is, could use C++ classes, and, the most important for me, could be compiled into C++ or some DLL/.SO (plus its .h) so that I could link it into my C++ program and make use of the classes the script defines or implements.

我知道我可以嵌入任何流行的脚本语言,例如 lua、ruby、python……但接口通常包含某种eval"函数,用于评估提供的脚本代码.根据用于耦合 C++ 和脚本语言的工具,将脚本回调集成到 C++ 中可能或多或少容易编写,但我还没有看到任何脚本语言实际上允许我编写独立的模块以 .h 和 .so/dll 的形式公开给我的程序(可能与生成 C++ 代码的脚本语言一致).

I know I could embed any popular scripting language such as lua, ruby, python... but the interface usually includes some kind of "eval" function that evaluates the provided scripting code. Depending on the tool used to couple C++ and the scripting language, the integration for callbacks of the script into C++ could be more or less easy to write, but I haven't seen any scripting language that actually allows me to write independent modules that are exposed as a .h and .so/dll to my program (maybe along the lines of a scripting language that generates C++ code).

你知道任何这样的工具/脚本语言吗?

Do you know any such tool/scripting language?

提前致谢.

PD.我一直在思考 Vala 或 Haskell 的 GHC.它们生成 C,但不生成 C++...

PD. I've been thinking along the lines of Vala or Haskell's GHC. They generate C, but not C++...

推荐答案

UPDATE 2020:今天我可能会选择 Lua + Sol2/3 除非我真的想避免使用 Lua 作为语言.Chaiscript 在这种情况下成为一个很好的候选者,尽管与 Lua+Sol2/3 相比它在性能方面不是最佳的(尽管多年来它已经有了很大的改进,所以在许多情况下它仍然足够好).猎鹰已经死了好几年了,RIP.

UPDATE 2020: Today I would probably go with Lua + Sol2/3 except if I really want to avoid Lua as a language. Chaiscript becomes a good candidate in this case though it is not optimal performance-wise compared to Lua+Sol2/3 (though it was greatly improved through years so it is still good enough in many cases). Falcon have been dead for some years, RIP.

以下是比语言绑定更面向 C++ 集成的:

The following ones are more C++ integration oriented than language bindings :

  • ChaiScript - 目前正在一个小项目中尝试,有趣的是,这个是用 C++ 制作的只需包含一个标题就可以记住和工作!不确定它是否适合一个大项目,但会看到,尝试一下!
  • (不再维护) Falcon - 尝试一个大项目,非常棒;它不是像 ChaiScript 那样的一个包含嵌入",而是因为它非常灵活,并且完全被认为可以在 C++ 中使用(libs 中只有 C++ 代码)――我决定在我最大的项目中坚持使用它,需要很多脚本灵活性(与 ruby??/python 相当)
  • AngelScript - 还没试过
  • GameMonkey - 还没试过
  • Io - 还没试过
  • ChaiScript - trying at the moment in a little project, interesting, this one is MADE with C++ in mind and works by just including a header! Not sure if it's good for a big project yet but will see, try it to have some taste!
  • (not maintained anymore) Falcon - trying on a big project, excellent; it's not a "one include embed" as ChaiScript but it's because it's really flexible, and totally thought to be used in C++ (only C++ code in libs) - I've decided to stick with it for my biggest project that require a lot of scripting flexibility (comparable to ruby/python )
  • AngelScript - didn't try yet
  • GameMonkey - didn't try yet
  • Io - didn't try yet

对您来说,如果您真的想用 C++ 编写脚本模块并轻松地将其公开给脚本语言,我建议您使用 Falcon.它完全是用 C++ 编写的,所有的模块/库都是这样编写的.

For you, if you really want to write your scripting module in C++ and easily expose it to the scripting language, I would recommand going with Falcon. It's totally MADE in C++, all the modules/libraries are written that way.

相关文章