Re: ffi can't call function in my executable

  • From: GrrrWaaa <grrrwaaa@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sat, 2 Jun 2012 14:29:07 -0700

I'm certainly no expert on this, but I have a working example in Linux of what 
you are doing, and the build script goes something like the following. The 
-whole-archive option is set for the application object files (where your foo() 
is), and unset for the libraries to avoid unwanted bloat. OTOH if you also want 
to FFI to those libraries, you might need to use -whole-archive for them too.

g++ -c -O3 -Wall -fPIC -ffast-math <includes> <source files>
g++ -rdynamic -Wl,-whole-archive <object files> -Wl,-no-whole-archive <static 
libraries (including luajit)> -o <application name>



On Jun 2, 2012, at 1:15 PM, Dan Eloff wrote:

> Sorry on this laptop keyboard I find I'm frequently muddling things (I
> think it's the trackpad which I've figured out how to disable now)
> 
> The code was as you say in the lua file "ffi.C.foo(5)", and the body
> of foo is defined as "return bar * bar;" (square the int bar)
> 
> But the error persists and I can think of nothing else to try.
> 
> Many thanks,
> Dan
> 
> On Sat, Jun 2, 2012 at 3:38 PM, GrrrWaaa <grrrwaaa@xxxxxxxxx> wrote:
>> 
>> On Jun 2, 2012, at 11:18 AM, Dan Eloff wrote:
>> 
>>> I declared in lua.cpp:
>>> 
>>> extern "C" int foo(int bar)
>>> {
>>>    return bar *en in lua bar;
>> 
>> Not sure what that means?
>> 
>>> }
>>> 
>>> Then in lua:
>>> 
>>> local ffi = require("ffi")
>>> ffi.cdef[[
>>> int foo(int bar);
>>> ]]
>>> local barreturn = lib.foo(5)
>> 
>> Replace above line with:
>> 
>> local barreturn = ffi.C.foo(5)
>> 
>>> io.write(barreturn)
>>> io.write('\n')
>>> 
>>> And I load and execute this via luaL_dofile in main.cpp
>>> 
>>> Which produces the error: "undefined symbol: foo"
>>> 
>>> I added -rdynamic to gcc flags and running "nm -g mybinary | grep foo" I 
>>> get:
>>> 
>>> 00000000004089fc T foo
>>> 
>>> I'm running on linux with latest "stable" luajit 2 and gcc++ 4.7
>>> 
>>> Cheers,
>>> Dan
>>> 
>> 
>> 
> 


Other related posts: