2
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.)

le_me
  • 3,089
  • 3
  • 26
  • 28

1 Answers1

2

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • thank you for looking into this! unfortunately this doesn't work for me.. my ```nm``` output: [link](https://gist.github.com/flo-l/df5a0b8c5f63772b0c7d) – le_me Nov 03 '14 at 16:46
  • For what it's worth, I created a fresh Ubuntu install, installed rustc 0.13.0-nightly (ceeac26de 2014-11-04 21:26:23 +0000), installed ruby 2.1.3p242, and ffi (1.9.6). I added `pub` and tweaked the Ruby code to print stuff and it worked just fine. – Shepmaster Nov 05 '14 at 16:11
  • I also reinstalled everything and now it works! thank you for heroic effort!! – le_me Nov 05 '14 at 16:30
  • That `nm` tool is amazing! I was able to discover that my `no_mangle` annotation was not in the right place, leading to mangled function names in the lib. – bright-star Aug 10 '15 at 07:03