lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2013/3/7 steve donovan <steve.j.donovan@gmail.com>:

> Interesting that one of the reasons Go is getting traction on servers
> comes from the single statically-linked executable it generates.

It is not as well known as it deserves to be (i.e. the Wiki has no article
containing the word "executable" and a title that makes you think
that's it) that making a single statically-linked executable from Lua
source is quite easy.

I append a "hello, world" demo. Just change the part defining your
application. Note the whitespace before the closing double-quotes.

Then compile it with the same command that your system uses
to compile the Lua standalone interpreter, and you are done.
For example, on Linux it's

    cc hello.c -llua -lm -ldl -o hello
#include <stddef.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

static char* LuaCode = 
"print'Hello, world!' "
"io.write('? ') "
"input = io.read() "
"print'Read the full manual, please.' "  
"return "  
;

int main() {
  int err;
  lua_State *L=luaL_newstate();
  if (!L) {
    fprintf(stderr,"Could not start program, not enough memory.\n");
    return -1; }
  luaL_openlibs(L);
  err = luaL_loadstring(L,LuaCode);
  if (err==LUA_ERRSYNTAX) fprintf(stderr,
    "Invalid Lua code. Contact the program author.\n");
  if (err==LUA_ERRMEM) 
    fprintf(stderr,"Could not load program, not enough memory.\n");
  if (err!=LUA_OK) return 100+err; 
  err = lua_pcall(L, 0, 0, 0);
  if (err) fprintf(stderr,"--> %s\n",lua_tostring(L,-1));
  lua_close(L);
}