简体   繁体   中英

Compile lua5.2 files to one with using require and dofile

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM