首先给库中定义一个函数
static int sys_tick(lua_State *L) {
long long time = system_tick();
lua_pushinteger(L, time);
return 1;
}
之后将函数与lua中用的函数名称关联起来
static const struct luaL_Reg system_lib_f[] = {
{ "tick", sys_tick},
{ NULL,NULL } };
最后将函数和库的名称注册到lua中就行了
#define SYSTEM_META_NAME "system"
int luaopen_system(lua_State *L) {
luaL_newlib(L, system_lib_f);
return 1;
}
luaL_requiref(L, SYSTEM_META_NAME, luaopen_system, 1);
于是我们在lua中就可以调用这个库函数了
local t = system.tick()
print('time = ',t)