2

How can I build and compile my own Lua files on Windows? And make them executable.

I am reading Beginning Lua programming, and I have Windows 7 and MacOS Lion both installed. I am having the hard time to follow the instructions. They do not work for me.

On MacOS I open the terminal and put these in:

  1. export LUA_DIR=/usr/local/lib/lua/5.1
  2. mkdir -p /usr/local/lib/lua/5.1 (it tells me, mkdir: illegal option) and I can not follow from here
  3. SET LUA_DIR=”c:\program files\lua\5.1”

As for Windows I do this according to the book. This what I see in my shell c:\Users\bd>

  1. mkdir "c:\program files\utility" and it tells me access is denied

I have tried to right click on this folder and check off read only, but it does not work. Any clues would be appreciated, this part has been really confusing for me.

speeder
  • 6,197
  • 5
  • 34
  • 51
Bo Dash
  • 113
  • 1
  • 3
  • 10
  • See http://stackoverflow.com/questions/194520/creating-standalone-lua-executables and http://stackoverflow.com/questions/4459233/compiling-lua-script-to-standalone-executable – lhf Sep 17 '12 at 18:35

5 Answers5

3

To package your Lua files into an executable on Windows you have several options. There is srlua, there is wxLuaFreeze from wxLua (available as a binary for Windows), and there are more options in this SO answer.

Essentially, the main two options are: (1) append your Lua code to a precompiled exe file, such that it will be loaded and executed when that exe file is run, and (2) convert your Lua code into real executable by compiling it to bytecode, then to C, and then to your target platform.

As to your MacOS issue, mkdir -p means that mkdir is asked to create intermediate directories (for example, you asked to create /a/b/c, it will also create /a/b if those don't exist). As you don't say which version of MacOS you run, it's difficult to provide more detailed answer.

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
2

For now the standard distribution of Lua does not compile a script to native executable code; it execute your scripts by first compiling it to bytecode, then by interpreting the bytecode with a reasonnably fast static interpret (this also means that it is easily portable across native or virtual systems, and very resistant to attacks (that could be targetting bugs in the native compiler itself).

Also Lua still does not feature a runtime JIT compiler like Java and .Net: Lua still does not features a VM to produce a safe sandbox.

There exists Lua packages that convert your bytecode (or directly a source script) to a C source that can be used to convert a Lua library into native mode via the same C compiler used to compile the Lua engine itself (this is how the builtin libraries are produced, though they are slightly optimized manually in some time-critical parts).

However it is possible to compile Lua to a javascript source, and run it with fast performance using Javascript, because today's Javascript interprets do have good performance with their implemented VM featuring a JIT compiler for their own bytecodes.

It is also possible by converting it the Lua bytecode to a .Net or Java source that can then be executed directly from Lua (for that you need a version of Lua that has been ported to .Net or Java or Javascript, something that is not so complicate than developing in C/C++ directly a VM with a JIT compiler (a moderately complex part is the bytecode verifier, but the really complex part is the memory manager its garbage collector and its sandbox so that your Lua script will be fully isolated from the Lua engine itself for itw own memory, but the most complex part if the runtime optimizer and collection of profiling statistics: this has been done in the modern VMs for Java, .Net, Javascript, PHP/Zend, Python, Perl...).

I dont know which other language VM would offer the best performance to port Lua and implement on it a compiler to their own bytecode running at near native speed in their VM. But my own small experience with programs (in a much simpler language) self-generating a bytecode that they can run themselves, has always shown me Java winning in performance over .Net and Javascript. This is most probably because Java features an profiling-based dynamic code optimizer

(On the opposite the .Net optimizer runs only once during program installation, using some profiling data collected during the installation of the .Net VM itself, or at first instanciation of the script, without really knowing any profiling data collected during execution of the compiled program itself, and based on some cheked assumptions about the platform capabilities).

I also don't if would be faster in PHP, Python or Perl; the comparison with newer Javascript engines was never attempted though. Porting/compiling a Lua program to Javascript is relatively easy because it implements closures relatively easy for the resolution of linkages. Then the generated Javascript will compile to native code with the excellent Javascript's JIT compilers we have today (and never cease to improve in performance, so much that I've seen various appliactions running now faster in Javascript than before when they were written in C++ or plain C; as well the memory footprint has largely been reduced, we no longer have memory leaks, and even if there's a garbage collector, today's Javascript VM have a very efficient one, which is even better than the GC implemented in the native Lua).

But Lua remains useful as it is easy to secure and sandbox and offers various security benefits (but there are security issues in Lua as well for some kinds of applications, where Javascript offers some solutions, notably for side-channel attacks based on variation of time of execution; but these side-channel attacks are very hard to solve and can affect any system, any program, any programming language, and this starts becoming a critical issue because they are now more esily exploitable; the reason of that comes from hardware optimizations that we depend more and more today when we want to maximize the performances). And with Lua you may be more immune to these problems that a sandboxing sofware environment cannot solve alone.

Probably later we'll see a true VM implementation of Lua with a JIT and self-generating code and the possibility to instanciate new sandboxed VMs to run their self-generated code. It will take more time to generate an EXE file for distribution; notably because it generally requires adding also an installer and a distribution manager.

So for now we could imagine distributing Lua applications compiled to the bytecode of another JIT-capable VM: this generated bytecode would be faster than the Lua bytecode, and would then be extremely complex to reverse-engineer to the semantics of Lua because it would require two separate reverse engineering first from the bytecode of the other VM to the bytecode of Lua, both bytecodes loosing some easiy inferable rules and options tested and foll, and then again to sme Lua source

verdy_p
  • 1,615
  • 1
  • 20
  • 14
1

For the OSX terminal issue:

This command should work

export LUA_DIR=/usr/local/lib/lua/5.1

This command will probably give you permission problems:

mkdir -p /usr/local/lib/lua/5.1

You may try this to solve that. You will be prompted for your password:

sudo mkdir -p /usr/local/lib/lua/5.1

This command has nothing to do with OSX and will not work. This is a windows command:

SET LUA_DIR=”c:\program files\lua\5.1”
sstendal
  • 3,148
  • 17
  • 22
  • curl -R -O http://www.lua.org/ftp/lua-5.2.1.tar.gz---------------- tar zxf lua-5.2.1.tar.gz --I have tried this,but it gives me an error on opening the file. – Bo Dash Sep 17 '12 at 17:17
0

You have a permissions problem with Windows- try creating your cmd or PowerShell in Administrator mode. C:\Program Files is a protected directory that a regular user account doesn't have permission to write to.

As for the OS X issue, check out the mkdir OS X manual page to make sure you have the command correct.

David B
  • 2,688
  • 18
  • 25
0

So, if I understood your question correctly, you are trying to build Lua on Windows.

This is of course possible, but not easy for beginners. I would highly recommend you to use a binary distribution, which is much easier to install, unless you have special requirements.

Here are several Windows distributions :

prapin
  • 6,395
  • 5
  • 26
  • 44
  • -1: "if I understood your question correctly" Then you didn't understand correctly. This is about making Lua scripts into executable programs, not about compiling Lua. – Nicol Bolas Sep 17 '12 at 18:39
  • Maybe. But then, I don't really understand the `LUA_DIR` and the `mkdir` statements, nor the `curl -R -O` comment. – prapin Sep 17 '12 at 19:11
  • Well my main goal is to make my main.lua into executable files .and in order to do that,since I am new, I was following instruction on the "Beginning Lua programming" Book.and I was unable to follow it. – Bo Dash Sep 17 '12 at 19:15