简体   繁体   English

使用require和dofile将lua5.2文件编译为一个

[英]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. 我可以将所有文件编译为一个文件,然后从C执行。在lua文件中,我使用require和dofile函数。 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. 当我尝试使用luac进行编译,然后要执行已编译的文件时,它将无法找到在已编译文件中构建的模块。 I thought the including lua files via require and dofile function luac compiler proccess like for example javascript compiler do. 我认为通过require和dofile函数luac编译器过程(例如javascript编译器)来包含lua文件。 In simply terms Javascript compilers adding the importing files to one. 简而言之,JavaScript编译器将导入文件添加到一个文件中。

And is it good practise when you do a release for app have the lua scripts in one file? 当您在应用程序发行版中将lua脚本保存在一个文件中时,这是一种好习惯吗? 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. 因为我有一些带有lua脚本的目录,并且在发布时,我必须具有可读格式的所有lua文件和二进制文件。

luac is not a packaging tool, but simply a compiler. luac不是打包工具,而仅仅是编译器。 It will compile everything you feed it -- as long as it's valid Lua -- into Lua bytecode 它将把您提供的所有内容(只要它是有效的Lua)编译为Lua字节码

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. 如果您需要将所有内容都保存在一个文件中,并且希望使用require包含模块,则必须稍稍修改源文件,以便require知道在内存中的位置。

Simply put this at the end of all Lua module files: 只需将其放在所有Lua模块文件的末尾即可:

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

Then simply require or luaL_dofile your combined Lua right after your Lua init. 然后只需在Lua初始化之后luaL_dofile requireluaL_dofile合并的Lua。

Whatever you do with dofile , it is probably easier to just rewrite your code to functions. 无论您使用dofile做什么,将代码重写为函数都可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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