【发布时间】:2018-01-21 04:57:00
【问题描述】:
我想用 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
但我不知道如何将其解释为调试。
【问题讨论】:
-
也许尝试“add.dll”而不是“add”?
-
文档说luajit.org/ext_ffi_api.html它会寻找.dll,但我什至尝试过.so(虽然我在Windows上)也无济于事。
-
我对 C 语言比较陌生,尤其是动态库,我是否认为我错过了程序集中的某些内容或 C 脚本中的某些内容。
-
您使用的是什么平台? Windows、Linux 还是其他?
-
@CircArgs - 您是否从库中导出函数
add?