参考: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函数
参考: 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
导入路径为 绝对路径