Tuesday, December 6, 2016

Running the Lua (5.2) C API in Ubuntu 14

Took me some time to figure this out trying out this example.

First you need to install Lua:

ubuntu> sudo apt-get install lua5.2
ubuntu> sudo apt-get install liblua5.2-dev

Then compile the source file:

ubuntu> gcc a.c -I/usr/include/lua5.2 -L/usr/lib/x86_64-linux-gnu -llua5.2

I had to make some adjustments in the source file. The adjusted source file is as follows:

#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void) {
char buff[256];
int error;
lua_State *L = luaL_newstate(); /* opens Lua */
luaL_openlibs(L);
while (fgets(buff, sizeof(buff), stdin) != NULL) {
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
}
lua_close(L);
return 0;
}
view raw a.c hosted with ❤ by GitHub