1

Can I compile all my files to one and then execute from C. In my lua files I am using require and dofile functions. When I try to use luac for compiling and then I want to execute the compiled file then It will not be able to find my modules which are built in the compiled file. I thought the including lua files via require and dofile function luac compiler proccess like for example javascript compiler do. In simply terms Javascript compilers adding the importing files to one.

And is it good practise when you do a release for app have the lua scripts in one file? Because I have a few directories with lua scripts and when I do a release I must have all lua files with binary in readable form.

  • BTW, in LuaJIT (that's Lua 5.1, not Lua 5.2 as you need) there is a way to compile any Lua module to bytecode and generate object file `luajit -b test.lua test.obj` which can be linked with your host application written on C, so this module is stored inside executable and is accessible with `require("test")` as if the module were usual external file. – Egor Skriptunoff Feb 23 '18 at 10:55
  • Yes I heard about luajit but I think it is a step back if you must downgrade instead upgrade to lua5.3. Or you think it is good aproach to use older lua. – Ondrej Prochazka Feb 23 '18 at 15:22
  • It depends on what you really need: a couple of new features in the language or 3x speed. – Egor Skriptunoff Feb 23 '18 at 15:25
  • Honestly? 3x speed. So that is convinced me. I'll try it only i am a little bit affraid of migrating. Thanks – Ondrej Prochazka Feb 23 '18 at 16:03
  • Well Luajit also has a significantly larger binary/runtime (though still small compared to other languages) and compiles on _significantly_ fewer platforms - seeing as normal Lua compiles on every C99 platform – dualed Feb 24 '18 at 06:45

1 Answers1

0

luac is not a packaging tool, but simply a compiler. It will compile everything you feed it -- as long as it's valid Lua -- into Lua bytecode

If you need to have everything in one file and want to include your modules using require, you have to modify your source files a bit, so that require knows where to find those modules in memory.

Simply put this at the end of all Lua module files:

package.loaded["<yourmodnamehere>"] = yourmodreturnvalue

Then simply require or luaL_dofile your combined Lua right after your Lua init.

Whatever you do with dofile, it is probably easier to just rewrite your code to functions.

dualed
  • 10,262
  • 1
  • 26
  • 29