您的位置:首页 > 编程语言 > Lua

使用C语言扩展lua语言功能

2017-11-13 10:55 441 查看
lua是openresty(nginx+lua)中重要的部分,当原生lua满足不了需求时,便需要用C语言对lua进行扩展,比如下面我写了自己的时间模块

#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <lua_openresty/lua.h>
#include <lua_openresty/lauxlib.h>
#include <lua_openresty/lualib.h>

static int l_yourownname(lua_State *L)
{
...这里写自己的代码
}

static const struct luaL_Reg yourownname2[] = {
{"yourownname", l_yourownname},
{NULL, NULL}
};

extern int luaopen_yourownname3(lua_State* L)
{
luaL_newlib(L,yourownname2);return 1;
}


将这个文件编译成动态链接库的a.so文件,最后在lua上调用它

local a = require "a";
local des = libencode.yourownname();
print(des)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: