加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua使用ffi调用c程序的函数

发布时间:2020-12-14 21:45:28 所属栏目:大数据 来源:网络整理
导读:参考:?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数

参考:?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;

double get_size()
{
    if (getrlimit(RLIMIT_CORE,&rlmt) == -1) {
        return -1;
    }
    return (double)rlmt.rlim_cur;

};

double 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 (double)rlmt.rlim_cur;

};

g++ 编译一下,变成 *.so 文件

module(...,package.seeall)

x_ffi = require("ffi");

set_get_core = x_ffi.load(/home/.../my_corefile.so)

local _M = {}

x_ffi.cdef[[
    double get_size();
    double set_size(double CORE_SIZE);
]]

function _M.to_set_size(size)
    return set_get_core.set_size(size);
end

function _M.to_get_size()
    return set_get_core.get_size();
end

-- function _M.to_cd_chdir(path)
--     return set_get_core.cd_chdir(path);
-- end
return _M

导入路径为 绝对路径

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读