我想用 C 编写一些函数以在 Lua 中使用,我认为我能找到的最简单的方法是使用 LuaJIT 的 FFI。
我有一个 C 文件“add.c”:
int add(int a, int b){
return a+b;
}
我将它组合成“add.o”:
gcc -c add.c
我制作“add.dll”:
gcc - shared -o add.dll add.o
最后,我尝试在 LuaJIT 中运行以下 Lua 代码:
local ffi =require("ffi")
local test=ffi.load("C:\\users\\quebe\\Desktop\\add")
ffi.cdef[[
int add(int a,int b);
]]
print(test.add(1,2))
并得到:
luajit: test.lua:3: cannot load module 'C:\users\quebe\Desktop\add': %1 is
not a valid Win32 application.
stack traceback:
[C]: in function 'load'
test.lua:3: in main chunk
[C]: at 0x7ff72be120c0
但我不知道如何解释它以进行调试。
最佳答案
根据 this , 在加载 dll 之前应该有 C 函数的声明:
local ffi =require("ffi")
ffi.cdef[[
int add(int a, int b)
]]
local test=ffi.load("C:\\users\\quebe\\Desktop\\add")
附录:
此外,正如 Egor Skriptunoff 所提到的,dll 文件中的函数应该声明为导出的。详细信息在 this 中给出所以回答。
关于c - LuaJIT FFI加载dll错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45656120/