weixin_39869959
weixin_39869959
2020-12-26 10:40

Unable to build "Call Rust from Ruby" example on Mac

Hi!

I've been trying to build the example of calling Rust from Ruby from the readme and have hit some issues.

The first snag I hit was that the feature in the readme wasn't detected:


error: failed to select a version for `rutie`.
    ... required by package `rutie_ruby_example v0.1.0 (/Users/me/code/rutie_ruby_example)`
versions that meet the requirements `= 0.7.0` are: 0.7.0

the package `rutie_ruby_example` depends on `rutie`, with features: `no-link` but `rutie` does not have these features.

If I remove the feature from the toml file, I get an error about not being able to find the static libruby, which is correct as I built ruby dynamically as requested by the readme:


  = note: ld: library not found for -lruby-static

If I set the feature using enviroment variables NO_LINK_RUTIE, I get complaints at the link stage that certain symbols cannot be found.


...
  = note: Undefined symbols for architecture x86_64:
            "_rb_funcallv", referenced from:
                rutie::binding::vm::call_method::h4f58f42a73be3784 in librutie-d43da9d278d1d079.rlib(rutie-d43da9d278d1d079.rutie.83fl08uz-cgu.15.rcgu.o)
            "_rb_intern2", referenced from:
                rutie::binding::symbol::internal_id::h53c0d0a300c24aed in librutie-d43da9d278d1d079.rlib(rutie-d43da9d278d1d079.rutie.83fl08uz-cgu.8.rcgu.o)
            "_rb_cObject", referenced from:
                rutie::util::inmost_rb_object::h58fc3e7371a9bd30 in librutie-d43da9d278d1d079.rlib(rutie-d43da9d278d1d079.rutie.83fl08uz-cgu.5.rcgu.o)
                rutie::class::class::Class::superclass_to_value::h623835c60d0cf15e in librutie-d43da9d278d1d079.rlib(rutie-d43da9d278d1d079.rutie.83fl08uz-cgu.8.rcgu.o)
            "_rb_string_value_cstr", referenced from:
                rutie::binding::string::value_to_string::h31a08fe870ac65fa in librutie-d43da9d278d1d079.rlib(rutie-d43da9d278d1d079.rutie.83fl08uz-cgu.14.rcgu.o)
...

The linker command itself doesn't actually seem to have libruby in it:


"cc" "-m64" "-L" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.13gzxphx1qkh09zh.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.13ohqgh1e9065748.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.176ggxngm7z90af0.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.19905pc63lyfea4z.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.1sqrydyuz8kxg43z.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.1tf1904qivzwgynr.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.1ujbqjz2queip6ud.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.1zlj8q95ije0uldn.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.28ij0jl8wx7v4dlu.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.2pll6j6yd9e1px0r.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.2qp6kmprupu21ngf.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.2w54erubrjse8vbk.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.33b3mnyngfhtsb0l.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.3dpzfxpvnrwk7suk.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.3njal17w5pffbvwh.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.3o52on0z8tomm3zt.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.3shfgz0k9oan8kx.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.3xjqdc41yoxkrjac.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.4knkv9rrmqqlxs71.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.4lm03l44oubherxf.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.4o6b8l2818e0y5uk.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.4qbt5kzv3r5cu9au.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.4yxyva8ajkrg5lj6.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.551s5p1koaimc597.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.5d7940jh37m9hg9v.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.5diqvg2jjyqhpnew.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.622gamq7rq49gcz.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.d0fe6qr8d5rqwu2.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.eepjz2ustpmdm6y.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.imntb66b3x452wh.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.nxzdpzkjoi0fsp8.rcgu.o" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.wt0do9sj9kmt9nv.rcgu.o" "-o" "/Users/me/code/rutie_ruby_example/target/debug/deps/librutie_ruby_example.dylib" "-Wl,-exported_symbols_list,/var/folders/g1/p9stlry52vg5syw1zjzsd5dr0000gn/T/rustcxeo8Vw/list" "/Users/me/code/rutie_ruby_example/target/debug/deps/rutie_ruby_example.4hd42g73j2fxvw4s.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/me/code/rutie_ruby_example/target/debug/deps" "-L" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/me/code/rutie_ruby_example/target/debug/deps/librutie-d43da9d278d1d079.rlib" "/Users/me/code/rutie_ruby_example/target/debug/deps/liblibc-2f697bcb367999fc.rlib" "/Users/me/code/rutie_ruby_example/target/debug/deps/liblazy_static-a489a9dd01075252.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-ce17fe05fa88fff7.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-88250736fdfd488b.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-b4c23b5d831b61ea.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-52678edf67c3cbfc.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace-aa90723925b767c3.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace_sys-1ad31f0e1f5330bc.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-572bf915a75bf178.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-709b6ef89ca6e6af.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-174360cf12ee1390.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-45e1eff5d23696f7.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-d67c1d4ca132d737.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-41d0f9161f0005e8.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-f090ec23084537ee.rlib" "/Users/me/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-7c4cbb62cd53c96a.rlib" "-lSystem" "-lresolv" "-lc" "-lm" "-dynamiclib" "-Wl,-dylib"

I have made sure that the DYLD_LIBRARY_PATH variable is set to the correct location as well accoring to the readme, but that has not helped.

Please let me know if I'm doing something wrong! 🙂

该提问来源于开源项目:danielpclark/rutie

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • weixin_39935257 weixin_39935257 4月前

    Hey Rupert, I did manage to get it working, without the hassle as you've mentioned. I just make sure the my ruby is correctly configured by CONFIGURE_OPTS=--enable-shared rbenv install 2.7.0 and use the master branch of rutie. One thing I could think of for your case is, maybe you've got multiple ruby installed, that when actually linking, it might go with the system ruby or other versions.

    点赞 评论 复制链接分享
  • weixin_39935257 weixin_39935257 4月前

    FYI, I was working my project on Linux until yesterday I compiled it on macOS. I just ran into the same issue as in your original post. By searching around, I ended up using the same flags defined here https://github.com/wasmerio/ruby-ext-wasm/blob/master/.cargo/config , which works for me

    点赞 评论 复制链接分享
  • weixin_39935257 weixin_39935257 4月前

    Hey , I just ran into the same problem using version 0.7.0. By looking around in the repo, I realized that the change for adding the no-link feature is not part of version 0.7.0 yet. It looks like Daniel has made some progress on version 0.8.0 last year, but not actually released it. So, I guess before that, we can just use the latest master.

    点赞 评论 复制链接分享
  • weixin_39869959 weixin_39869959 4月前

    Hi , Did you manage to get the example working with the latest master?

    After some hackery (manually setting linker flags to the ruby library directory and setting -lruby manually ) I got a binary that immediately segfaulted when I tried to load the rust code. In order to get the rust code to compile, I also needed to comment out the build.rs code of rutie (to force it to take the NO_LINK path).

    点赞 评论 复制链接分享
  • weixin_39869959 weixin_39869959 4月前

    After digging some more, it seems as though it is the first call to the ruby from rust that is causing the segfault, the code here: https://github.com/danielpclark/rutie/blob/master/src/binding/class.rs#L13 .

    This implies to me that some contract is being broken but I don't know enough about the ruby VM to really understand what is going on there, but at first glance there is nothing obviously wrong. I found this resource which could be helpful: https://ruby-hacking-guide.github.io/class.html , and will leave this for this evening. 💤

    点赞 评论 复制链接分享

相关推荐