相信积累的力量

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
posted @ 2022-09-23 12:21  ThreeF  阅读(27)  评论(0编辑  收藏  举报

相信积累的力量