Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple dylib linking fails. #19680

Closed
ghost opened this issue Dec 10, 2014 · 8 comments
Closed

Simple dylib linking fails. #19680

ghost opened this issue Dec 10, 2014 · 8 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries

Comments

@ghost
Copy link

ghost commented Dec 10, 2014

version: rustc 0.13.0-nightly (8bca470 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
@ghost
Copy link
Author

ghost commented Dec 10, 2014

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

@kmcallister kmcallister added the A-linkage Area: linking into static, shared libraries and binaries label Dec 10, 2014
@alexcrichton
Copy link
Member

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.

@ghost
Copy link
Author

ghost commented Dec 10, 2014

Ah, that solves it. Thank you!

@ghost ghost closed this as completed Dec 10, 2014
@ghost
Copy link

ghost commented Apr 29, 2015

@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

@alexcrichton
Copy link
Member

@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.

@ghost
Copy link

ghost commented Apr 29, 2015

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

@eltiare
Copy link

eltiare commented Aug 27, 2016

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

@cdecompilador
Copy link

But wait, doing the rlib statically links everything? Lets say I have a binary wich has as dependency just libA which is "dylib", the expected behaviour should be that the generated executable shouldn't be able to run without the libA.so, but adding the "rlib" makes it static link, why? and why bevy doing some weird trick has dynamic linking working properly with just "dylib" option?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries
Projects
None yet
Development

No branches or pull requests

4 participants