Libstd undefined symbols when building dylib

#1

I’m making a library which I want to call from C/++. I have a main rust crate (rlib), then a simple wrapper crate that wraps the main crate’s functionality using extern “C” functions. If I build the wrapper crate as a staticlib I can link against it from C++ just fine, and everything works as expected. If I change my crate type to dylib though I get a whole bunch of undefined symbols when trying to link to it from C++:

/usr/bin/ld: warning: libstd-d52e1299537ab398.so, needed by 
/home/anders/packages/rama/0.1.0/lib/librama.so, not found (try using -rpath or -rpath-link)
/home/anders/packages/rama/0.1.0/lib/librama.so: undefined reference to `std::io::stdio::Stdout::lock::h25c724b0429bb7d6'
... etc etc x100...

I don’t see the libstd-d52e1299537ab398.so anywhere in my target directory, although I do see some .so’s for dependent crates (e.g. serde).

#2

You should set your crate type to cdylib.

#3

That did indeed work, thanks! I’d thought the cdylib was for something else.

#4

To make it more clear, here’s an extract from reference.

  • bin: ordinary executable.
  • lib: a library to be linked statically into Rust programs.
  • dylib: a library to be linked dynamically into Rust programs.
  • staticlib: a library to be linked statically into non-Rust programs.
  • cdylib: a library to be linked dynamically into non-Rust programs.

(plus rlib as mainly internal format and proc_macro, used, well, for procedural macros)

#5

Thanks, that’s very clear. I realise now that the page I’d found as an example through google: https://doc.rust-lang.org/1.5.0/book/rust-inside-other-languages.html is woefully out of date…