c - LuaJIT FFI加载dll错误

标签 c lua ffi luajit

我想用 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/

相关文章:

c - Lua 脚本转 C++ 代码

c++ - 为 C++ 选择嵌入式脚本语言

fortran - 对 `_gfortran_cpu_time_4' 的 undefined reference

windows - Windows 上的 flutter : how to call an external dll?

c - -C 中 printf 中的 NaN

c++ - 当子进程不刷新其标准输出时如何从子进程读取标准输出?

memory - Lua 表 : how to assign value not address?

haskell - 如何使用 Stack/Cabal 构建早期部分的程序输出作为同一构建后续部分的源?

c - 读取可能包含换行符的标准输入的最佳方式?

c - lmbench 如何使用 C 测量 L1 和 L2 缓存延迟? (无法理解手册中的解释)