In LuaJIT, the so is loaded through the fii library, and the functions in the so are called, but in the loaded so, the variables used by some methods are defined through extern. For example, the actual definition of this structure is in another so file. At this time, loading so will report undefined symbol: how to solve this problem?
local ffi = require("ffi")
print("start test:")
package.cpath = package.cpath .. ";/data/system2.so"
ffi.cdef[[
typedef enum
{
E_DEBUG_LEVEL_ERR,
E_DEBUG_LEVEL_WARNING,
E_DEBUG_LEVEL_NOTICE,
E_DEBUG_LEVEL_INFO,
E_DEBUG_LEVEL_MAX
}DebugLevel_E;
typedef enum
{
COMRET_SUCCESS = 0,
COMRET_INTERNAL_ERROR = 1,
COMRET_INVALID_ARGUMENTS = 2,
COMRET_RESOURCE_EXCEEDED = 3,
COMRET_LOCK_TIMEOUT = 4,
COMRET_MAX
} ComRet;
typedef void json_object;
extern g_eDebugLevel;
ComRet Reboot(json_object *jsonSend);
]]
local example = ffi.load("/data/system2.so",true)
local jsonSend = ffi.new("json_object")
local ret = example.Reboot(jsonSend)
if ret == ffi.C.COMRET_SUCCESS then
local jsonString =
ffi.string(ffi.C.json_object_to_json_string(jsonSend))
print("JSON Object content:", jsonString)
else
print("Reboot failed or invalid return value")
end
the actual definition of this structure is in another so file
FFI is unable to read structure definitions from so files.
You must explicitly specify your structure in full details in ffi.cdef
argument.
Usually this could be done by adapting the definition from your header file.
You can define an opaque struct (by specifying forward declaration which lacks definition)
typedef struct json_object json_object;
It is enough for receiving such an object created somewhere else, but you will be unable to create object of this type with ffi.new
.
Sometimes it is enough to create a memory block (for example, ffi.new"uint64_t[1000]"
or something like that) large enough to fit the object which structure is unknown.