lua使用ffi调用c程序的函数
参考: https://blog.csdn.net/weiwangchao_/article/details/16880401
http://luajit.org/ext_c_api.html
https://www.cnblogs.com/darkknightzh/p/5812763.html
lua 调用 C,需要用到 lua 的 ffi 库,它允许从纯Lua代码调用外部C函数,使用C数据结构,但是C的数据类型并不一定都能转化成lua的数据类型。
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <dirent.h>
struct rlimit rlmt;
char* get_size()
{
if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {
return -1;
}
return (char*)rlmt.rlim_cur;
};
char* set_size(double CORE_SIZE)
{
rlmt.rlim_cur = (rlim_t)CORE_SIZE;
rlmt.rlim_max = (rlim_t)CORE_SIZE;
if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
return -1;
}
return (char*)rlmt.rlim_cur;
}; int cd_chdir(const char *dirname)
{
int a=chdir(dirname);
return a;
};
【g++ 编译一下,变成 *.so 文件 】
下面为 Makefile 文件, 在目录下 make 能直接生成 *.so 文件 ,修改里面的 *.so *.o 文件
## Linux/BSD
LDFLAGS += -shared
CFLAGS ?= -g -O -Wall
override CFLAGS += -fpic
#CC = gcc
RM = rm -f
COPY = cp
all: my_corefile.so
my_corefile.o: my_corefile.c
my_corefile.so: my_corefile.o
$(CC) $(LDFLAGS) $^ -o $@
clean:
$(RM) *.so *.o
lua 文件
local ffi = require 'ffi'
--local ffi_string = ffi.string
--local ffi_new = ffi.new
--local C = ffi.C ffi.cdef[[
int get_size();
int set_size(long CORE_SIZE);
int cd_chdir(const char *dirname);
]]
set_get_core = ffi.load('/home/chentao/c_test/c_lua/qq/mycorefile/my_corefile.so')--直接导入绝对路径
local a = set_get_core.get_size()
print(a) local b = set_get_core.set_size(524280)
print(b)
print("hass set")
local a = set_get_core.get_size()
print(a)
print(os.getenv("PWD"))
print(" cd ",set_get_core.cd_chdir("/home"))
导入路径为 绝对路径
lua使用ffi调用c程序的函数的更多相关文章
- (原)lua使用ffi调用c程序的函数
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5812763.html 参考网址: http://luajit.freelists.narkive.co ...
- C程序与Lua脚本相互调用
Lua脚本是一种可用于C程序开发/测试的工具,本篇介绍一下C程序与Lua脚本如何进行相互调用,更加详细的操作参见<Programing in Lua>.本文分为3个部分:1.Windows ...
- [转载]C#控制台应用程序里调用自己写的函数的方法
(2011-08-15 15:52:13) 转载▼ 标签: 转载 分类: 技术类 原文地址:C#控制台应用程序里调用自己写的函数的方法作者:萧儿 最近写程序,遇到了一个很白痴的问题,记录下来,免得下次 ...
- Lua 中的string库(字符串函数库)总结
(字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...
- Lua语法基础(2)--基本语法、函数
上一篇编辑编辑着,发现,缩进出了问题.作为一个不是强迫症的人,实在是忍受不了同一级内容不同缩进方式的槽点,于是重开一篇吧.(万幸,这样的文章也只有我自己看.) 第四 基本语法 赋值语句,Lua可以对多 ...
- Lua中的常用语句结构以及函数
1.Lua中的常用语句结构介绍 --if 语句结构,如下实例: gTable = {} ] ] then ]) == gTable[] then ]) else print("unkown ...
- lua 15 协程-协同程序
转自:http://www.runoob.com/lua/lua-coroutine.html 什么是协同(coroutine)? Lua 协同程序(coroutine)与线程比较类似:拥有独立的堆栈 ...
- C#中可直接调用WIN32的API函数--USER32.DLL
Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...
- mfc 调用Windows的API函数实现同步异步串口通信(源码)
在工业控制中,工控机(一般都基于Windows平台)经常需要与智能仪表通过串口进行通信.串口通信方便易行,应用广泛. 一般情况下,工控机和各智能仪表通过RS485总线进行通信.RS485的通信方式是半 ...
随机推荐
- leetcode2. 两数相加
使用迭代的方式 class Solution{ public: ListNode *addTwoNumbers(ListNode* l1,ListNode *l2) { ListNode *res=) ...
- 快速调用Android虚拟机
由于使用Android studio开发flutter 每次打开软件都需要很长时间,所以我自己使用vscode进行开发,没有虚拟机也开发不了,于是就有了这篇博客的作用啦 在本地桌面新建一个xxx.ba ...
- Redis和MySQL数据同步及Redis使用场景
1.同步MySQL数据到Redis (1) 在redis数据库设置缓存时间,当该条数据缓存时间过期之后自动释放,去数据库进行重新查询,但这样的话,我们放在缓存中的数据对数据的一致性要求不是很高才能放入 ...
- Redis面试题(46题)
1.什么是Redis?简述它的优缺点? Redis 的全称是:Remote Dictionary.Server,本质上是一个 Key-Value 类型的内存数据库,很像memcached,整个数据库统 ...
- [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- [LeetCode] 79. Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- find square root
public class Solution { public static void main(String[] args) { Scanner ip = new Scanner(System.in) ...
- 日均5亿查询量的京东订单中心,为什么舍MySQL用ES?
阅读本文大概需要 8 分钟. 来源:京东技术订阅号(ID:jingdongjishu) 作者:张sir 京东到家订单中心系统业务中,无论是外部商家的订单生产,或是内部上下游系统的依赖,订单查询的调 ...
- 如何同时在Isilon的所有网卡上抓取网络包?
命令行如下: cd /ifs/data/Isilon_Support/ mkdir $(date +%m%d%Y) isi_for_array 'for i in `ifconfig | grep - ...
- 用pyenv管理Python多版本及下载加速方法--Mac上
原文:https://www.jianshu.com/p/91fc7ecc5e46 先大致介绍下pyenv的安装及配置流程.随后介绍加速下载方法 安装: brew install pyenv 配置 在 ...