h3rald
h3rald

Reputation: 923

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?

Upvotes: 43

Views: 63500

Answers (8)

darkfrei
darkfrei

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

nestedbytes
nestedbytes

Reputation: 1

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

Upvotes: 0

polm23
polm23

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

tonypdmtr
tonypdmtr

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.

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.

Upvotes: 1

mad nick
mad nick

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

Ignacio
Ignacio

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

Edwin Jarvis
Edwin Jarvis

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

Mike F
Mike F

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

Related Questions