Creating standalone Lua executables
Is there an easy way to create standalone .exe files from Lua scripts? Basically this would involve linking the Lua interpreter and the scripts.
I believe it is possible (PLT Scheme allows the creation of standalone executables in the same way), but how, exactly?
Check out for srlua. It does what you need.
It's from one of the Lua authors. On this address there is also pre-compiled Windows binaries, so that would be even easier for you I think.
To make executable from script use bin2c utility such way:
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)
Example with 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.
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;
}
Besides the above suggestions, you can take a look at L-Bia.
It can make standalone executables including lua scripts and the needed dynamic libraries.
As of 2018, luastatic is an actively maintained tool that does this. srlua from the accepted answer and the other tools on this page seem to be unmaintained.
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.
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.
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