dhilst

Creating C libraries/extensions/binds to Lua

I'm training the C API of lua.. planning to create extensions to awesome window manager (the one I use) -> http://awesome.naquadah.org/. Here is a "Lua calling C" hello world.


/*
* File: hellolib.c
*
* Compile:
* gcc -Wall -fPIC -c hellolib.c && gcc -shared -Wl -o libhellolib.so hellolib.o
*
* Calling from lua:
*
* > hello_lib = package.loadlib("/home/geckos/programming/lua/libhellolib.so", "lua_open_hellolib")()
* >
* >
* > hello_lib.hello_from_c()
* Hello from C world to lua
* >
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>


static int hello_from_c(lua_State *L)
{
puts("Hello from C world to lua");
return 0;
}

static const struct luaL_reg hello_lib[] = {
{ "hello_from_c", hello_from_c },
{ NULL, NULL },
};

int lua_open_hellolib(lua_State *L)
{
luaL_openlib(L, "hello_lib", hello_lib, 0);
return 1;
}