Lua 调用自定义C模块(练习)
2015-12-25 15:39
681 查看
代码:hello.c
gcc hello.c -fPIC -shared -o hello.so
test.lua
http://www.linuxidc.com/Linux/2014-09/106763.htm?utm_source=tuicool&utm_medium=referral
#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
相关文章推荐
- lua遍历调用C#泛型List、IEnumberable、Dictionary
- casper#evaluate传参数问题
- Tasks and Back Stack(任务和返回栈)
- lua
- LUA与C++交互第一篇
- 纯Lua实现Rc4加密
- 纯Lua实现的Base64
- 纯Lua实现的位操作
- WEB项目整合spring-data-hbase出现NoSuchMethodError: PageContextImpl.evaluateExpression
- luacurl安装
- <1>Lua 环境搭建与语法基础
- lua练手基础
- Lua加密
- lua-function
- Lua学习笔记(5) : 取得table內变量
- Lua-require
- 不同版本Lua介绍
- Lua包管理工具Luarocks介绍
- lua让我的师兄不敢拿8k的薪水
- lua类与对象