首页 文章

Luajit ffi如何在time.h中调用函数?

提问于
浏览
1

我尝试以这种方式调用函数 tan math.h (直接复制声明)并且它有效:

local ffi = require("ffi")
ffi.cdef[[
    double tan(double x);
]]
print(ffi.C.tan(45))

但是当我试图以同样的方式调用 time.hlocaltime 函数时:

local ffi = require("ffi")
ffi.cdef[[
    struct tm *localtime(const time_t *tp);
]]
print(ffi.C.localtime(1234544))

并得到错误:

lua: C:\Users\xiang\Desktop\bm.lua:4: declaration specifier expected near 'time_t'
stack traceback:
    [C]: in function 'cdef'
    C:\Users\xiang\Desktop\bm.lua:4: in main chunk
    [C]: at 0x00401f00
[Finished in 0.1s with exit code 1]

我已经检查了官方手册thisthis,但仍然感到困惑 .

2 回答

  • 2

    您希望从FFI调用的每个函数都需要先定义 . 如果不是LuaJIT不如何解析FFI函数调用,如何进行从Lua到C(反之亦然)等的数据类型转换 .

    保持这一点,为了使你的代码工作,你需要定义 time_tstruct tm . time_t 通常定义为有符号整数 . 您可以在localtime docs(man localtime)中找到 struct tm 的定义 .

    ffi.cdef[[
       struct tm {
          int tm_sec;    /* Seconds (0-60) */
          int tm_min;    /* Minutes (0-59) */
          int tm_hour;   /* Hours (0-23) */
          int tm_mday;   /* Day of the month (1-31) */
          int tm_mon;    /* Month (0-11) */
          int tm_year;   /* Year - 1900 */
          int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
          int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
          int tm_isdst;  /* Daylight saving time */
       };
       struct tm *localtime(const int32_t *tp);
    ]]
    

    另外,函数 localtime 需要一个指针值,而不是一个常量整数 . 因此有必要将存储整数的c数据指针传递给 localtime . 这有一种LuaJIT习语 .

    local time = ffi.new("int32_t[1]")
    time[0] = 1234544
    local tm = C.localtime(time)
    

    由于C中的数组和指针虽然不完全相同,但在大多数情况下是可以互换的 .

    最后,您无法直接打印 struct tm . 应将其存储到变量中并打印出您感兴趣的字段 .

    print(tm.tm_sec)
    
  • 0

    您不能使用 time_t ,因为它不是本机C类型 . 将其替换为适当的本机类型或使用相应的struct typedef . 然后它应该工作 .

相关问题