如何嵌入一个C二进制中的一个Lua脚本? [英] How to embed a Lua script within a C binary?

查看:124
本文介绍了如何嵌入一个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\"\n"
  "print(x)\n";
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文件(或它的编译后的字节code,这是更快和更小的),那么你可以传递到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 /窗口之外。

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天全站免登陆