嵌入时如何使用LuaJIT的ffi模块?

发布于 2024-11-05 14:33:47 字数 1224 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的绿洲 2024-11-12 14:33:47

ffi 库需要 luajit,因此您必须使用 luajit 运行 lua 代码。
来自文档:
“FFI 库紧密集成到 LuaJIT 中(它不能作为单独的模块提供)”。

如何嵌入luajit?
看看这里 http://luajit.org/install.html 下的“嵌入 LuaJIT”

在 mingw 下运行你的示例如果我

__declspec(dllexport) int barfunc(int foo)

在 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

__declspec(dllexport) int barfunc(int foo)

at the barfunc function.

Under Windows luajit is linked as a dll.

心凉 2024-11-12 14:33:47

正如 Misianne 指出的,您需要导出该函数,如果您使用 GCC,则可以使用 extern 来完成此操作:

extern "C" int barfunc(int foo)
{
    /* a dummy function to test with FFI */ 
    return foo + 1;
}

如果您在使用 GCC 的 Linux 下遇到未定义符号的问题,请注意链接器通过将 -rdynamic 标志传递给 GCC,将所有符号添加到动态符号表中:

g++ -o 应用程序 soure.cpp -rdynamic -I... -L... -llua

As misianne pointed out, you need to export the function, which you can do by using extern if you are using GCC:

extern "C" int barfunc(int foo)
{
    /* a dummy function to test with FFI */ 
    return foo + 1;
}

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:

g++ -o application soure.cpp -rdynamic -I... -L... -llua

坠似风落 2024-11-12 14:33:47

对于那些尝试使用 VC++(2012 或更高版本)在 Windows 上使用 C++ 编译器使其工作的人:

  • 请确保使用 .cpp 扩展名,因为这将进行 C++ 编译,
  • 使该函数具有外部 C 链接,以便 ffi 可以链接到它,使用 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:

  • make sure you use the .cpp extension, as this will do C++ compilation
  • make the function have external C linkage so that ffi can link to it, with extern "C" { ... }
  • export the function from the executable, with __declspec(dllexport)
  • optionally specify the calling convention __cdecl, not required because should be it by default and not portable
  • wrap the Lua headers in an extern "C" { include headers }, or better just #include "lua.hpp"

    #include "lua.hpp"  
    
    extern "C" {
    __declspec(dllexport) int __cdecl barfunc(int foo) { 
     return foo + 1;
    }}
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文