Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

compilation - How to compile Lua scripts into a single executable, while still gaining the fast LuaJIT compiler?

How can I compile my Lua scripts into a single executable file, while also gaining the super fast performance benefits of LuaJIT?

Background:

  • My Lua scripts are for a web application I created (e.g. to host http://example.com)
  • My current technology stack is NGINX (web server), Lua/LuaJIT (language to retrieve dynamic content)
  • I have around 50+ .lua files that make up my web application (from Models/Views/Controllers)
  • FreeBSD 9 operating system

For simplicity sake in deployment, I'd like to compile down all of my .lua scripts that run my web application down to a single executable.

  1. Is this possible and how?

    It appears that Lua official comes with a library called SRLua

  2. What are the negatives to compiling down my .lua to a single executable (e.g. would performance be worse, etc)?
question from:https://stackoverflow.com/questions/11317269/how-to-compile-lua-scripts-into-a-single-executable-while-still-gaining-the-fas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Translate all of the Lua source code files to object files and put them in a static library:

for f in *.lua; do
    luajit -b $f `basename $f .lua`.o
done
ar rcus libmyluafiles.a *.o

Then link the libmyluafiles.a library into your main program using -Wl,--whole-archive -lmyluafiles -Wl,--no-whole-archive -Wl,-E.

This line forces the linker to include all object files from the archive and to export all symbols.

For example, a file named foo.lua can now be loaded with local foo = require("foo") from within your application.

Details about the -b option can be found on Running LuaJIT.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...