lua的c扩展
ubuntu% cat hello.c
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
static int hello(lua_State *L){
printf("hello\n");
return 0;
}
static const struct luaL_Reg hello_lib [] = {
{"hello", hello},
{NULL, NULL}
};
int luaopen_hello(lua_State *L){
luaL_newlib(L, hello_lib);
return 1;
}
ubuntu% gcc -shared -fPIC -o hello.so hello.c
ubuntu% lua
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
> t = require("hello")
> t
table: 0x563bd8678f20
> t.hello()
hello