lua_State *L;
void initLuaTest();
void init_lua_engine()
{
L = lua_open();
luaL_openlibs(L);
initLuaTest();
}
void close_lua_engine()
{
lua_close(L);
}
$ cat test.c
#include <stdio.h>
#include <lua5.1/lua.h>
#include <lua5.1/lauxlib.h>
#include <lua5.1/lualib.h>
static int l_get_test_msg(lua_State* state)
{
lua_pushstring(state, "C message");
return 1;
}
static const struct luaL_reg l_test_lib[] =
{
{"getTestMsg", l_get_test_msg},
{NULL, NULL}
};
int main(int argc, char* argv[])
{
lua_State* state = lua_open();
luaL_openlibs(state);
luaL_register(state, "test", l_test_lib);
int err = luaL_dostring(state, "print(test.getTestMsg())");
if (err) {
fprintf(stderr, "%s\n", lua_tostring(state, -1));
}
lua_close(state);
return 0;
}
$ gcc -Wall test.c -llua5.1
$ ./a.out
C message
Now I'm not sure if I am totally wrong here but here is the Lua code I have for the above:
This just simply does not work and spits back some error message about test not existing basically. All help would be appreciated, thanks!