c語言源代碼:
#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static int hello_sin(lua_State *L){
double d = luaL_checknumber(L, 1);
lua_pushnumber(L, sin(d));
return 1;
}
static const struct luaL_Reg hello_lib[] = {
{"hello_sin" , hello_sin},
{NULL, NULL}
};
//luaopen_xxx 系列函數為lua的hook函數,會執行並注冊我們自己的類庫
int luaopen_hello_lib(lua_State *L){
/*luaL_newlib(L, hello_lib);*/
luaL_register(L, "hello_lib",hello_lib); // lua 5.1
return 1;
}
編譯腳本:
#!/bin/bash gcc a.c -fPIC --shared -o hello_lib.so
使用部分:
[lua]
local N = require(“hello_lib”)
N.hello_sin(1)
[/lua]