分享

c

 quasiceo 2014-01-12

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.

asked May 7 '11 at 7:37
jagttt
4541518

1  
There's some docs here /ext_ffi.html - hth –  daven11 May 7 '11 at 7:55

 
Well I've checked that and it didn't cover my problem :( –  jagttt May 7 '11 at 9:10

 
You'll probably do way better posting this on the lua mailing list, which Mike Pall actively monitors, and will probably post an answer –  Necrolis May 7 '11 at 9:46

 
If you get an answer from the mailing list it wouldn't hurt to post it here too (where it is more likely to be found with a Google search.) –  finnw May 7 '11 at 10:35
add comment

3 Answers

up vote 8 down vote accepted

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:///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.

answered May 7 '11 at 12:33
misianne
1405

add comment

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

answered Apr 22 '12 at 19:20

add comment

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;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章