【发布时间】:2011-08-20 15:44:57
【问题描述】:
我正在尝试将 LuaJIT 嵌入到 C 应用程序中。代码是这样的:
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
int barfunc(int foo)
{
/* a dummy function to test with FFI */
return foo + 1;
}
int
main(void)
{
int status, result;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
/* Load the file containing the script we are going to run */
status = luaL_loadfile(L, "hello.lua");
if (status) {
fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
exit(1);
}
/* Ask Lua to run our little script */
result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
exit(1);
}
lua_close(L); /* Cya, Lua */
return 0;
}
Lua 代码是这样的:
-- Test FFI
local ffi = require("ffi")
ffi.cdef[[
int barfunc(int foo);
]]
local barreturn = ffi.C.barfunc(253)
io.write(barreturn)
io.write('\n')
它报告如下错误:
Failed to run script: hello.lua:6: cannot resolve symbol 'barfunc'.
我四处搜索,发现 ffi 模块上的文档很少。非常感谢。
【问题讨论】:
-
这里有一些文档luajit.org/ext_ffi.html - hth
-
嗯,我已经检查过了,它没有解决我的问题:(
-
你可能会在 lua 邮件列表上发布这个更好的方法,Mike Pall 积极监控它,并且可能会发布答案
-
如果您从邮件列表中获得答案,也可以将其发布在此处(通过 Google 搜索更有可能找到。)