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

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

2015-12-25 15:39 681 查看
代码: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: