41

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?

h3rald
  • 903
  • 1
  • 7
  • 16

7 Answers7

32

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.

Edwin Jarvis
  • 5,980
  • 6
  • 36
  • 41
  • Sadly, srlua didn't work for me.The solution with luac/bin2c below worked – Zane Jan 11 '13 at 10:54
  • 1
    srlua distributes binaries in their github repo- https://github.com/LuaDist/srlua These worked pretty well for me- `glue.exe srlua.exe my.lua my.exe` – noahp May 29 '15 at 15:55
10

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;
}
  • 4
    Note that bin2c was dropped with Lua 5.1, so you need another way to package up the bytecode and put it on the Lua stack. See http://lua-users.org/wiki/BinToCee for a reimplementation in Lua. – Charles Stewart Feb 07 '14 at 10:35
7

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.

Ignacio
  • 1,115
  • 1
  • 7
  • 12
6

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.

polm23
  • 14,456
  • 7
  • 35
  • 59
  • so, is there any tool which can 1. find all needed packages and DLLs, then copy all of them automatically? 2. pack the Lua source files into EXE, so there is low-level protection of the source code. Just like what [pyinstaller](https://www.pyinstaller.org/) does. So any soft? Thanks – oyster Jul 23 '19 at 15:34
4

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
0

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.

Link: Windows console utility to convert in a single step a Lua source with its Lua source dependencies into a C file ready for compilation.

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.

tonypdmtr
  • 3,037
  • 2
  • 17
  • 29
0

Here is a easy method to do:

  1. Download the bins of lua from http://luabinaries.sourceforge.net/
  2. Extract that file and make a app.lua in the same folder
  3. Open run by WIN key plus R and type iexpress.
  4. In the iexpress select Create new Self Extraction Directive file and press next.
  5. Select Extract files and run an installation command and press next.
  6. Give the name of your app.Select no prompt and do not display a license and then when it asks you to select a file then chose the folder you have your app.lua and those lua bins
  7. Select all and press next In the install program type "lua54.exe app.lua" (without the "") Select default in show window and in the finished message select no message 11.In package name and options, click browse and type the name of your exe and then select hide extracting progress and store file using long file name Select no restart and then you can save the sed file or not Select next and next it should make a exe u can double click on it to run the app. Drawbacks: People can easily find your source code by renaming the file to .zip after the .exe
shour
  • 1
  • 2