IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    c语言扩展lua

    istrone发表于 2016-07-28 02:33:36
    love 0

    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]

    Maybe you like these:
    c语言下简单的PHP扩展
    简单几行的电脑病毒
    c语言程序模块化
    cgi脚本语言环境变量
    C语言中的预处理指令
    无觅


沪ICP备19023445号-2号
友情链接