compiler-constructionlualuac

Lua crashes when programs are compiled


I'm working on a very, very simple Lua program, trying to teach myself the language. Here's the three line program:

file = io.open("hello.txt", "w")
file:write("Hello, World.\n")
io.close(file)

When I use the program with Lua as a script, it works fine. When I compile the program, it works. However, when I run the compiled program, I get this:

alt text http://profnano.org/andy/misc/img/lua_crash.jpg

I'm using Lua 5.1.2 on Windows XP on a Pentium 4. I uninstalled Lua and re-installed it, with no effect. I'm compiling the script as follows:

luac -o hello.exe hello.lua

Am I doing something wrong, or is this a known bug? I'd like to be able to write Lua programs to run on computers without Lua installed. Help! (And thanks!)


Solution

  • luac doesn't compile to a system exe file. All it does is take the lua script, and turn it into lua bytecode. You still have to pass the resulting file to lua to execute.

    So the exe file contains garbage from the OS point of view. When exe files don't contain valid exe headers, the OS for some reason decides that it must be an old style dos .com file and tries to execute it with the 16bit dos subsystem. Hence the strange error message.

    If you want to make an exe out of compiled lua you will need to use a different environment - perhaps visual studio - to make a small project that loads (and executes) a lua file - compiled or script - using an embedded lualib.

    Creating Standalone Lua Executables on Stackoverflow has some tips on how to go about that.