Rust: Simple dylib linking fails.

Created on 10 Dec 2014  路  7Comments  路  Source: rust-lang/rust

version: rustc 0.13.0-nightly (8bca470c5 2014-12-08 00:12:30 +0000)
os: manjaro linux (arch)

main.rs
extern crate test_crate;

fn main() {
    test_crate::foo();
}
test_crate.rs
#[no_mangle]
pub fn foo() {
    println!("Hi");
}
output
$ rustc --crate-type=dylib test_crate.rs
$ rustc -L . main.rs 
error: cannot satisfy dependencies so `std` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `rand` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `rustrt` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `core` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `alloc` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `libc` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `collections` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `unicode` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: aborting due to 8 previous errors
A-linkage

Most helpful comment

@Russell91 you may want to be sure that this is part of your Cargo.toml:

[lib]
# ...
crate-types = ["rlib", "dylib", "bin"]

That way the binary being generated should favor the rlib and you'll also have a dylib available to you. Otherwise I think that error message is likely working as intended, but I'd have to take a peek at the project to make sure.

All 7 comments

Assuming I'm using the flags correctly, '-C prefer-dynamic':

$ rustc -L . -C prefer-dynamic main.rs
error: cannot satisfy dependencies so `std` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `rand` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `rustrt` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `core` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `alloc` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `libc` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `collections` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `unicode` only shows up once
note: having upstream crates all available in one format will likely make this go away
error: aborting due to 8 previous errors

Building a dynamic library defaults to statically linking all dependencies, so test_crate has static versions of the standard library and friends which confuses the compiler. If you compile test_crate with -C prefer-dynamic you should be good to go.

Ah, that solves it. Thank you!

@alexcrichton I'm not able to tell cargo to use -C prefer-dynamic. As a result, it's not possible to use cargo with both rust libraries and c shared libraries. Changing my project to use rustc for these activities is not really an option either because the rust ecosystem is so heavily biased towards using cargo. Is it really not possible to build a rust shared library that compiles against both *.so and *.rlib components?

Edit: Nevermind. I read your comment a little more closely and realized a subtlety. I'm building both a dylib and a binary file with my cargo file, and it was the binary file that was creating the problems when I changed the objective to building a shared library. Removing src/main.rs fixed the error messages. Here is what I was getting:

error: cannot satisfy dependencies so std only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so core only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so collections only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so rustc_unicode only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so alloc only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so libc only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so rand only shows up once
help: having upstream crates all available in one format will likely make this go away
error: aborting due to 7 previous errors

@Russell91 you may want to be sure that this is part of your Cargo.toml:

[lib]
# ...
crate-types = ["rlib", "dylib", "bin"]

That way the binary being generated should favor the rlib and you'll also have a dylib available to you. Otherwise I think that error message is likely working as intended, but I'd have to take a peek at the project to make sure.

@alexcrichton, just adding "rlib" to the list of crate-type fixed the problem.

Note that it's crate-type and not crate-types

Was this page helpful?
0 / 5 - 0 ratings