如何在 C 二进制文件中嵌入 Lua 脚本? [英] How to embed a Lua script within a C binary?

查看:27
本文介绍了如何在 C 二进制文件中嵌入 Lua 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我可以做的外壳世界中被宠坏了:

I've been getting spoiled in the shell world where I can do:

./lua <<EOF
> x="hello world"
> print (x)
> EOF
hello world

现在我正在尝试在 C 应用程序中包含一个 Lua 脚本,我希望它会随着时间的推移而增长.我从一个简单的开始:

Now I'm trying to include a Lua script within a C application that I expect will grow with time. I've started with a simple:

const char *lua_script="x="hello world"
"
  "print(x)
";
luaL_loadstring(L, lua_script);
lua_pcall(L, 0, 0, 0);

但这有几个缺点.首先,我必须转义换行符和引号.但是现在我遇到了 字符串长度 '1234' 大于长度 '509' ISO C90 编译器需要支持 警告,同时使用 gcc 进行编译,我想不仅保留这个程序自包含但可移植到其他编译器.

But that has several drawbacks. Primarily, I have to escape the line feeds and quotes. But now I'm hitting the string length ‘1234’ is greater than the length ‘509’ ISO C90 compilers are required to support warning while compiling with gcc and I'd like to keep this program not only self-contained but portable to other compilers.

在 C 程序中包含大型 Lua 脚本而不是作为单独的文件发送给最终用户的最佳方法是什么?理想情况下,我想将脚本移动到一个单独的 *.lua 文件中以简化测试和更改控制,并以某种方式将该文件编译为可执行文件.

What is the best way to include a large Lua script inside of a C program, and not shipped as a separate file to the end user? Ideally, I'd like to move the script into a separate *.lua file to simplify testing and change control, and have that file somehow compiled into the executable.

推荐答案

一个非常便宜但不太容易改变的方法是使用 bin2c 之类的东西从选定的 lua 文件(或其编译的字节码,它更快更小),然后您可以将其传递给 lua 执行.

A really cheap, but not so easy to alter way is to use something like bin2c to generate a header out of a selected lua file (or its compiled bytecode, which is faster and smaller), then you can pass that to lua to execute.

您也可以尝试将其嵌入为资源,但我不知道它在 Visual Studio/Windows 之外是如何工作的.

You can also try embedding it as a resource, but I have no clue how that works outside of visual studio/windows.

根据您想要做什么,您甚至可能会发现 exeLua 的使用.

depending what you want to do, you might even find exeLua of use.

这篇关于如何在 C 二进制文件中嵌入 Lua 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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