Lua require ("ffi") reported an error

the code is as follows:

local ffi = require("ffi")
ffi.cdef[[
    struct timeval {
        long int tv_sec;
        long int tv_usec;
    };
    int gettimeofday(struct timeval *tv, void *tz);
]];
local tm = ffi.new("struct timeval");

report the following error after running:

2018/11/19 08:53:55 [error] 24046-sharp0: init_worker_by_lua_file error: lua/ngx_base/resty/utils/time/time_help.lua:24: attempt to redefine "timeval"
stack traceback:
    [C]: in function "cdef"
    lua/ngx_base/resty/utils/time/time_help.lua:24: in main chunk
    [C]: in function "require"
    lua/ngx_base/resty/utils/incr_help.lua:15: in main chunk
    [C]: in function "require"
    ...s/trade/third_party_union_payment/model/trade_tf_dao.lua:30: in main chunk
    [C]: in function "require"
    lua/modules/trade/task/trade_task.lua:14: in main chunk
    [C]: in function "require"
    lua/ngx_base/server/sys_task.lua:11: in function "init_task"
    lua/ngx_base/server/sys.lua:13: in function "init"
    ./lua/init_worker_by_lua.lua:16: in main chunk
Lua
Dec.08,2021

cause of the problem: after I installed luarocks install lua-resty-uuid, there is a definition of timeval in this file, so there will be an error!
solution:
1, uninstall luarocks remove lua-resty-uuid
2, and use the following methods

 ffi.typeof 

    local ffi = require "ffi"
    if pcall(ffi.typeof, "struct in_addr") then
        -- already defined! do nothing here...
    else
        -- undefined! let's define it!


        ffi.cdef[[
            struct in_addr {
                uint32_t s_addr;
            };
        ]]
    end

reference address

Menu