嵌入时如何使用LuaJIT的ffi模块?
我正在尝试将 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 模块的文档确实很少。多谢。
I'm trying to embed LuaJIT into a C application. The code is like this:
#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;
}
the Lua code is like this:
-- Test FFI
local ffi = require("ffi")
ffi.cdef[[
int barfunc(int foo);
]]
local barreturn = ffi.C.barfunc(253)
io.write(barreturn)
io.write('\n')
It reports error like this:
Failed to run script: hello.lua:6: cannot resolve symbol 'barfunc'.
I've searched around and found that there're really little document on the ffi module. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ffi 库需要 luajit,因此您必须使用 luajit 运行 lua 代码。
来自文档:
“FFI 库紧密集成到 LuaJIT 中(它不能作为单独的模块提供)”。
如何嵌入luajit?
看看这里 http://luajit.org/install.html 下的“嵌入 LuaJIT”
在 mingw 下运行你的示例如果我
在 barfunc 函数中添加。
在 Windows 下 luajit 作为 dll 链接。
ffi library requires luajit, so you must run lua code with luajit.
From the doc:
"The FFI library is tightly integrated into LuaJIT (it's not available as a separate module)".
How to embed luajit?
Look here http://luajit.org/install.html under "Embedding LuaJIT"
Under mingw your example run if i add
at the barfunc function.
Under Windows luajit is linked as a dll.
正如 Misianne 指出的,您需要导出该函数,如果您使用 GCC,则可以使用 extern 来完成此操作:
如果您在使用 GCC 的 Linux 下遇到未定义符号的问题,请注意链接器通过将 -rdynamic 标志传递给 GCC,将所有符号添加到动态符号表中:
As misianne pointed out, you need to export the function, which you can do by using extern if you are using GCC:
If you are experiencing problems with undefined symbols under Linux using GCC, take care to have the linker add all symbols to the dynamic symbol table, by passing the -rdynamic flag to GCC:
对于那些尝试使用 VC++(2012 或更高版本)在 Windows 上使用 C++ 编译器使其工作的人:
extern "C" { ... }
__declspec(dllexport)
__cdecl< /code>,不是必需的,因为默认情况下应该是它并且不可移植
将 Lua 头文件包装在
extern "C" { include headers }
中,或者更好只是#include "lua .hpp"
<前><代码>#include“lua.hpp”
外部“C”{
__declspec(dllexport) int __cdecl barfunc(int foo) {
返回 foo + 1;
}}
For those of you trying to make this work on Windows with VC++ (2012 or later), using the C++ compiler:
extern "C" { ... }
__declspec(dllexport)
__cdecl
, not required because should be it by default and not portablewrap the Lua headers in an
extern "C" { include headers }
, or better just#include "lua.hpp"