rubyrustffiruby-ffi

How do I use the ruby ffi gem to call a freestanding function in a rust library?


ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
rustc 0.13.0-nightly (f168c12c5 2014-10-25 20:57:10 +0000)

I want to use the ffi gem in conjunction with rust.

I have read this (quite outdated) blog post, which shows how to do that.

The problem is: it doesn't work.

Here is my code:

test.rs:

fn test(bla: i32) -> i32 { bla*bla }

#[no_mangle]
extern fn _test_wrapper(i: i32) -> i32 {
  test(i)
}

test.rb:

require 'ffi'

module Test
  extend FFI::Library
  ffi_lib File.absolute_path 'libtest.so'

  attach_function :_test_wrapper, [:int32], :int32
end

I compile test.rs like so:

rustc --crate-type dylib test.rs

And then

ruby test.rb

Output:

/home/me/.rvm/gems/ruby-2.1.2/gems/ffi-1.9.6/lib/ffi/library.rb:261:in `attach_function': Function '_test_wrapper' not found in [/home/me/Dokumente/ruby/rust_require/specs/test/libtest.so] (FFI::NotFoundError)
    from test.rb:7:in `<module:Test>'
    from test.rb:3:in `<main>'

What do I do wrong? (I already tried making it pub extern fn ..., doesn't work either.)


Solution

  • You were close, you just need to fix the warning you get when you compile the Rust code and make the function public:

    #[no_mangle]
    pub extern fn _test_wrapper(i: i32) -> i32 {
      test(i)
    }
    

    To help me debug the problem, I used nm to see what symbols the compiled library exports. I'm on OS X, so you might have to tweak arguments and filenames:

    $ nm -g libtest.dylib
    0000000000000e30 T __test_wrapper
    0000000000001020 S _rust_metadata_test_04c178c971a6f904
                     U _rust_stack_exhausted
                     U dyld_stub_binder
    

    Before marking the function as public, it did not show up in this list.