lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

At 03:50 PM 9/27/2011, Tim Caswell wrote:
>I have a folder full of lua modules (written in lua) and I want
>them embedded in my binary at compile/link time in such a way
>that the require system can find them.
>Is there a cross-platform way to embed these scripts and make
>them accessible to require?

I'm using Mathew Wild's Squish[1] along with luac and bin2c to accomplish this in one project. Squish collects (and can also discard whitespace and comments, and even GZip) all the module sources into one amalgamation .lua file that if executed preloads all the modules. I then use luac and bin2c to turn it into a constant array and code to load and execute it. That file is #included inline at a suitable point early in the program startup. After that point, all of the modules are sitting in package.preload and require just does the right thing. It turned a small utility that had been one .exe along with ten .lua files into a single .exe file, and made the downstream user much happier.


I ended up with a single .exe file because this utility statically linked its lua core and is not expected or required to be extensible with DLLs. Note that Squish cannot do anything about binary modules or supporting DLLs.

>I'm using luajit, but I think this question is generic.

I haven't tried this with LuaJIT, but I don't think there's too much magic going on in the Squished output. Squish is open source, so if there is an issue there is a chance it can be patched.

Ross Berteig                     
Cheshire Engineering Corp.