Re: ffi can't call function in my executable

  • From: Dan Eloff <dan.eloff@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 18 Jun 2012 21:41:45 -0500

I tried adding the whole-archive option to the linker, but the only
thing that seemed to work (or change anything looking at the readelf
-s output) was -Wl,-E which I stumbled upon by accident in the luajit
docs (for something unrelated.) Now foo shows up twice in readelf -s
and luaffi can find it.

I have no idea why it works or why -whole-archive worked for you and
not me. But it's enough for now so I can continue on. Thanks for your
help it got me looking in the right direction at least (and convinced
me that what I was trying to do was at least possible somehow.)

Cheers,
Dan

On Sat, Jun 2, 2012 at 4:29 PM, GrrrWaaa <grrrwaaa@xxxxxxxxx> wrote:
> 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: