为什么我们需要像Lua这样的可嵌入编程语言? [英] Why do we need an embeddable programming language like Lua?

查看:201
本文介绍了为什么我们需要像Lua这样的可嵌入编程语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用嵌入式编程语言的典型用例是什么?我是否正确理解这样的语言应该嵌入到某些程序环境中并应该可以从那里执行?

What are the typical use cases of using an embeddable programming language? Do I understand it correctly that such language should be embedded into some program environment and should be able to be executed from there?

推荐答案

由于您将问题标记为"Lua",因此我将在使用该语言的情况下为您提供答案.

Since you tagged the question as "Lua", I'll give you an answer in the context of this language.

Lua用C编写(几乎完全与C89标准兼容;如果需要,可以使用编译时开关轻松禁用不兼容的功能),并已设计为易于与C代码集成.在Lua的上下文中,集成"是指两个不同但相关的事物:

Lua is written in C (almost completely compatible with C89 standard; the incompatible features can be easily disabled, if needed, using compile-time switches) and has been designed to be easily integrated with C code. In the the context of Lua, "integrated" means two different, but related, things:

  1. 您可以轻松编写可被Lua代码用作库的C代码.可以通过将C代码静态或动态链接到Lua引擎的代码来实现集成.然后可以使用Lua require函数在您的Lua代码中引用链接的库.

  1. You can easily write C code that can be used as a library by Lua code. The integration is achieved either by static or dynamic linking your C code to Lua engine's code. The linked library can then be referred to in your Lua code using the Lua require function.

Lua引擎可以轻松地嵌入到C应用程序中,即(再次静态或动态地)链接到C应用程序代码.然后,C应用程序可以使用Lua的 C应用程序编程接口(Lua C -API).

Lua engine can be easily embedded in a C application, i.e. linked (again either statically or dynamically) to the C application code. Then the C application can interact with the Lua code using Lua's C application programming interface (Lua C-API).

注意:即使使用C ++应用程序,也可以花费更多的精力来做到这一点.

Note: this can be done, with a little more effort, also with a C++ application.

如果您的C应用程序将Lua嵌入很多(如果不是大多数),则可以将操作委派给Lua引擎,即,可以委派给使用C-API函数编写的代码,或者更好的是,可以将Lua代码委派给Lua引擎. Lua代码可以作为C字符串嵌入到您的C代码中,也可以存储为外部Lua脚本.

If your C application embeds Lua many, if not most, operations can be delegated to the Lua engine, i.e. either to code written using the C-API functions or, better yet, Lua code. Lua code could be embedded as C strings inside your C code or be stored as external Lua scripts.

使用Lua代码实现部分代码逻辑具有以下优点:

Having part of your code logic implemented using Lua code has several advantages:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆