windows 创建独立的 Lua 可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/194520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating standalone Lua executables
提问by h3rald
Is there an easy way to create standalone .exe files from Lua scripts? Basically this would involve linking the Lua interpreter and the scripts.
有没有一种简单的方法可以从 Lua 脚本创建独立的 .exe 文件?基本上这将涉及链接 Lua 解释器和脚本。
I believe it is possible (PLT Scheme allows the creation of standalone executables in the same way), but how, exactly?
我相信这是可能的(PLT Scheme 允许以相同的方式创建独立的可执行文件),但是究竟如何呢?
采纳答案by Edwin Jarvis
回答by Edwin Jarvis
To make executable from script use bin2c utility such way:
要使脚本可执行,请使用 bin2c 实用程序,如下所示:
luac script.lua -o script.luac
bin2c script.luac > code.c
Then create in text editor file main.c and compile/link it with your favorite compiler. That's it. (Note - executable also supports command line args)
然后在文本编辑器文件 main.c 中创建并使用您喜欢的编译器编译/链接它。就是这样。(注意 - 可执行文件也支持命令行参数)
Example with MSVC:
MSVC 示例:
cl /I "./" /I "$(LUA_DIR)\include" /D "_CRT_SECURE_NO_DEPRECATE" /D "_MBCS" /GF /FD /EHsc /MD /Gy /TC /c main.c
ld /SUBSYSTEM:CONSOLE /RELEASE /ENTRY:"mainCRTStartup" /MACHINE:X86 /MANIFEST $(LUA_DIR)\lib\lua5.1.lib main.obj /out:script.exe
mt -manifest $script.manifest -outputresource:script.exe;1
Use /SUBSYSTEM:WINDOWS for GUI executable. All that isn't easy just for the 1st time, you can create batch file to automate the process once you successfuly try it.
使用 /SUBSYSTEM:WINDOWS 作为 GUI 可执行文件。所有这一切都不是第一次那么容易,一旦成功尝试,您就可以创建批处理文件来自动化该过程。
main.c:
主文件:
#include <stdlib.h>
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(int argc, char *argv[]) {
int i;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_newtable(L);
for (i = 0; i < argc; i++) {
lua_pushnumber(L, i);
lua_pushstring(L, argv[i]);
lua_rawset(L, -3);
}
lua_setglobal(L, "arg");
#include "code.c"
lua_close(L);
return 0;
}
回答by Ignacio
回答by Ignacio
Since you say '.exe' I'll assume you're looking for a Windows solution. One idea is to just append scripts to a prebuilt interpreter executable. It may or may not qualify as 'easy' in your book.
既然您说的是“.exe”,我就假设您正在寻找 Windows 解决方案。一种想法是将脚本附加到预构建的解释器可执行文件中。在您的书中,它可能会也可能不会被称为“简单”。
The interpreter needs to be able to read itself, parse its header to determine where the regular .exe data ends (ie. where the script begins) and then hand off the remainder of the file to Lua.
解释器需要能够读取自己,解析它的头以确定常规 .exe 数据的结束位置(即脚本开始的位置),然后将文件的其余部分交给 Lua。
Other solutions don't require the interpreter to work as hard but do require more involved linking, whereas with this method, exeifying a script can be as simple as
其他解决方案不需要解释器那么努力,但需要更多涉及的链接,而使用这种方法,执行脚本可以像
copy interpreter.exe+script.lua script.exe
回答by polm23
回答by tonypdmtr
As this topic has somewhat 'perpetual' interest and the possible answers are 'fluid' in the sense that new solutions may emerge (while older ones may become obsolete), here's yet another possibility (for Windows) and pure Lua source dependencies.
由于该主题具有某种“永恒”的兴趣,并且从新解决方案可能会出现(而旧解决方案可能会过时)的意义上说,可能的答案是“流动的”,因此还有另一种可能性(对于 Windows)和纯 Lua 源依赖项。
链接:Windows 控制台实用程序,只需一步即可将 Lua 源及其 Lua 源依赖项转换为可供编译的 C 文件。
Note: Except where there are differences in the bytecode for others platforms, the resulting C code should be able to compile on any platform that can compile stock Lua source, and create stand-alone native binaries of your Lua applications.
注意:除了其他平台的字节码存在差异外,生成的 C 代码应该能够在任何可以编译库存 Lua 源代码的平台上编译,并创建 Lua 应用程序的独立本机二进制文件。