简体   繁体   English

在C ++中使用脚本语言

[英]Using scripting language in C++

My question is a little bit stupid but I decided to ask advanced programmers like some of you. 我的问题有点愚蠢,但我决定问像你们中的一些高级程序员。 So I want to make a "dynamic" C++ program. 因此,我想制作一个“动态” C ++程序。 My idea is to compile it and after compilation (maybe with scripting language like python) to change some how the code of the program. 我的想法是对其进行编译,然后再进行编译(可能使用诸如python之类的脚本语言)来更改程序代码。 I know you will tell me that after the compilation I can not change the code but is there a way of doing that. 我知道您会告诉我,编译后我无法更改代码,但是有这样做的方法。 Thank you! 谢谢!

You could design a modular architecture using plugins in external libraries. 您可以使用外部库中的插件来设计模块化体系结构。 For example you could use the Command Pattern . 例如,您可以使用命令模式 That way you can dynamically load code that was generated after you main program. 这样,您可以动态加载主程序后生成的代码。 You would have to fix an interface though. 您将不得不修复一个接口。 Functions like GetProcAddress in the Windows api might be a good point to start. Windows api中的诸如GetProcAddress之类的功能可能是一个不错的起点。

For dynamic coding and rapid prototyping I recommend to have a look at Lua . 对于动态编码和快速原型制作,我建议您看一下Lua The engine is very small and easy to integrate in your c++ program. 该引擎非常小,易于集成到c ++程序中。

The only way to do that in C++ is to unload the DLL with the code to be modified, modify the sources, invoke the compiler to regenerate the DLL, and reload the DLL. 在C ++中,这样做的唯一方法是使用要修改的代码卸载DLL,修改源代码,调用编译器以重新生成DLL,然后重新加载DLL。 It's very, very heavy weight, and it only works if the compiler is present on the machines where the code is to be run. 它的重量非常非常大,并且只有在要运行代码的计算机上存在编译器的情况下,它才起作用。 (Usually the case under Unix, rarely the case with Windows.) (通常在Unix下,在Windows下很少。)

Interpreted languages like Python are considerably more dynamic; 诸如Python之类的解释性语言具有更大的动态性。 Python has a built-in function to execute a string as Python code, for example. 例如,Python具有内置函数来将字符串作为Python代码执行。 If you need dynamically modifiable code, I'd suggest embedding Python in your application, and using it for the dynamic parts. 如果需要动态可修改的代码,建议将Python嵌入应用程序中,并将其用于动态部分。

Personally I have never played with re-compiling C++ during runtime, and I do not intend too. 就个人而言,我从未在运行时玩过重新编译C ++,也没有打算。 However I have been doing a lot of embedding of scripting languages lately. 但是,最近我一直在大量嵌入脚本语言。

Someone else mentioned the obvious first choice for embedding: Lua. 有人提到了显而易见的首选嵌入方式:Lua。 Lua is a pretty popular language to embed and you will find a bunch of documentation about how to do it. Lua是一种非常流行的语言,您可以找到很多有关如何使用它的文档。 Integrating it into the C++ will allow you to define behavior at runtime like you want. 将其集成到C ++中将使您可以在运行时定义所需的行为。

What I am using is a wonderful langauge called Squirrel. 我正在使用的是一种叫Squirrel的奇妙语言。 It is a lot like Lua but with native object(class) support and C++-like syntax. 它很像Lua,但具有本机对象(类)支持和类似C ++的语法。 I have managed to embed it into a C++ application, and through using the sqrat binding library both languages can share information easily. 我设法将其嵌入到C ++应用程序中,并且通过使用sqrat绑定库,两种语言都可以轻松共享信息。

I have squirrel building and initializing my UI. 我有松鼠建立和初始化我的用户界面。 As a result, 0 compiling is required in order to craft and structure my UI for my game. 结果,需要0编译才能为我的游戏制作和构造UI。 I intend to take this a step further and use this gameplay-side in order to create similar behavior you are looking for(changing behavior at runtime through dynamic code usage) 我打算更进一步,并使用此游戏方面来创建您正在寻找的类似行为(通过动态代码使用在运行时更改行为)

I recommend Checking out squirrel here: http://www.squirrel-lang.org/ 我建议在这里签出松鼠: http//www.squirrel-lang.org/

I plan on writing tutorials on how to embed squirrel and install the binding library, but I have not started on them yet. 我打算编写有关如何嵌入松鼠和安装绑定库的教程,但是我还没有开始使用它们。 If I can remember, I will edit this post in the future (could be a few months) once I have completed them. 如果我还记得的话,一旦完成,我将在以后(可能是几个月)编辑这篇文章。 In the meantime give it a shot yourself. 同时,自己动手试一试。 You may find it to your liking. 您可能会喜欢它。

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

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