Reputation: 923
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?
Upvotes: 43
Views: 63500
Reputation: 586
Download lua-5.4.2_Win64_bin.zip from https://sourceforge.net/projects/luabinaries/files/5.4.2/Tools%20Executables/
Unpack files to /lua-5.4.2_Win64_bin/:
lua54.dll
lua54.exe
Create my_script.lua with:
-- my_script.lua
-- Print a message to the console
print("Hello, Lua 5.4!")
-- Define a simple function
function add(a, b)
return a + b
end
-- Use the function and print the result
result = add(5, 7)
print("Result of adding 5 and 7:", result)
Create run_my_script.bat with code:
@echo off
lua54.exe my_script.lua
pause
Now you have files:
lua54.dll
lua54.exe
my_script.lua
run_my_script.bat
Run the run_my_script.bat, result:
Hello, Lua 5.4!
Result of adding 5 and 7: 12
Drücken Sie eine beliebige Taste . . .
Upvotes: 0
Reputation: 1
Here is a easy method to do:
Upvotes: 0
Reputation: 15633
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.
Upvotes: 7
Reputation: 3235
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.
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.
Upvotes: 1
Reputation:
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;
}
Upvotes: 11
Reputation: 1124
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.
Upvotes: 7
Reputation: 6140
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.
Upvotes: 31
Reputation:
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
Upvotes: 5