TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Luastatic – Build an executable from a Lua program

44 pointsby ers35over 9 years ago

7 comments

campermanover 9 years ago
You can do this in Luajit with the following trick:<p><pre><code> for f in *.lua; do luajit -b $f `basename $f .lua`.o done ar rcus libmylib.a *.o gcc -o myexe main-stub.c -I&#x2F;usr&#x2F;local&#x2F;include&#x2F;luajit-2.0 -L&#x2F;usr&#x2F;local&#x2F;lib -lluajit-5.1 -Wl,--whole-archive libmylib.a -Wl,--no-whole-archive -Wl,-E </code></pre> Where main-stub.c looks like this:<p><pre><code> #include &lt;lua.h&gt; #include &lt;lauxlib.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; int main(void) { int status, result, i; lua_State *L; L = luaL_newstate(); luaL_openlibs(L); &#x2F;* load your lua entry point here *&#x2F; status = luaL_loadfile(L, &quot;mainluafile.lua&quot;); if (status) { fprintf(stderr, &quot;Couldn&#x27;t load file: %s\n&quot;, lua_tostring(L, -1)); exit(1); } result = lua_pcall(L, 0, LUA_MULTRET, 0); if (result) { fprintf(stderr, &quot;Failed to run script: %s\n&quot;, lua_tostring(L, -1)); exit(1); } lua_close(L); return 0; } </code></pre> The executables produced are very small because you dynamically link to Luajit and statically link to your own Lua code. I have a full media scheduler running at 100 sites which is only 40k. And it runs like a demon on ARM too.
评论 #10452177 未加载
larmeover 9 years ago
For multiple lua files, you can first bundle them into a single lua file using squish.[0]<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;LuaDist&#x2F;squish" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;LuaDist&#x2F;squish</a>
fasteoover 9 years ago
Also check:<p>Bundle [1], using LuaJIT<p>Luvi [2], which is also able to generate a single file executable and you can include static resources.<p>[1] <a href="https:&#x2F;&#x2F;luapower.com&#x2F;bundle" rel="nofollow">https:&#x2F;&#x2F;luapower.com&#x2F;bundle</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;luvit&#x2F;luvi" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;luvit&#x2F;luvi</a>
unmoleover 9 years ago
Similar project for Python: <a href="http:&#x2F;&#x2F;www.pyinstaller.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.pyinstaller.org&#x2F;</a> Works with multiple source files and supports widely used Python modules out of the box.
motiejusover 9 years ago
Pretty good size: 214K (on amd64 Linux):<p><pre><code> $ ls -lhd hello -rwxr-xr-x 1 admin admin 214K Oct 26 09:53 hello $ ldd hello linux-vdso.so.1 (0x00007fff5f5ff000) libm.so.6 =&gt; &#x2F;lib&#x2F;x86_64-linux-gnu&#x2F;libm.so.6 (0x00007f10611e7000) libdl.so.2 =&gt; &#x2F;lib&#x2F;x86_64-linux-gnu&#x2F;libdl.so.2 (0x00007f1060fe3000) libc.so.6 =&gt; &#x2F;lib&#x2F;x86_64-linux-gnu&#x2F;libc.so.6 (0x00007f1060c39000) &#x2F;lib64&#x2F;ld-linux-x86-64.so.2 (0x00007f10614f6000) </code></pre> (edit: formatting)
hbbioover 9 years ago
This is an interesting project, thanks for sharing.<p>Did you explore the possibility to use LuaJIT instead? The ability to do this and also integrate with Terra <a href="http:&#x2F;&#x2F;terralang.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;terralang.org&#x2F;</a> would create a great project!
评论 #10450417 未加载
评论 #10450543 未加载
ifoundthetaoover 9 years ago
Ha! I remember you from the G-Wan forum days (:<p>I&#x27;m so excited to see you&#x27;re still active!