Lua 调用自定义C模块(练习)

来源:互联网 发布:淘宝如何处理中差评 编辑:程序博客网 时间:2024/06/06 19:21


代码:hello.c

#include <lua.h>#include <lauxlib.h>#include <math.h>#include <stdlib.h> /* For function exit() */#include <stdio.h> /* For input/output */#include<string.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>int port( lua_State *L ){    double d = luaL_checknumber(L, 1);    lua_pushnumber(L, sin(d));    return 1;}static const luaL_Reg hello[] = {    { "port", port },    { NULL, NULL }};int luaopen_hello( lua_State *L ){      luaL_newlib(L, hello);    return 1;}


gcc hello.c -fPIC -shared -o hello.so

test.lua

local hello = require("hello")print(hello.port(1))

http://www.linuxidc.com/Linux/2014-09/106763.htm?utm_source=tuicool&utm_medium=referral


0 0
原创粉丝点击