-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Linker error for no_std binary using musl #58163
Comments
Which Rust version are you using On latest nightly your example fails with:
Without |
Ah, I forgot to mention that I tried it with both 1.32.0 and the latest nightly. |
When using native no_std gnu target required symbols come from Manually adding So the question to Rust (libs?) team is whether Gist with linker args for different builds: https://gist.github.com/mati865/210dbf7f27b15ffa96bf9e0b79f5d0b5 |
This is expected behavior. This program is not linking If you want to statically link musl to your binary, the easiest way is to not make it a Sadly, solving this issue isn't trivial. I suppose we could expose a cargo feature from libc that allows users to force the linking of the C library such that you could use that here, but there are other things to consider here, like how this interacts with |
An alternative workaround that might be easier is to try adding this to your binary: #[link(name = "c", kind = "static", cfg(target_feature = "crt-static"))]
#[link(name = "c", cfg(not(target_feature = "crt-static")))]
extern {} |
I'm not sure if this is the right place to ask this, but is that available on stable? For me this only works when using But even with that, the workaround isn't working for me. On the
I don't know enough about how linking is done to try to get this to work, but maybe someone else knows what to do with this error. (Also, just to clarify: I don't actually need this for anything, I just stumbled across this behaviour when toying around with |
Nope, but neither are
I see
Makes sense - this is kind of a known issue, but is good to have a proper bug report for it, so thank you for trying so many things here! |
It's available on stable since 1.30, when
Ah, that's interesting. I tried it again with |
The problem is that we can't statically link musl to a binary twice, and libstd for musl always links musl statically. The only thing that the So when you have a This is bad, super hard to discover, etc and there should be a better way to do this. The simplest thing to do would be adding a new cargo feature to the So I think we probably want a more elaborated way of solving all these issues, that has nice error messages, and always works in the obvious way. I think, that if you add: #![feature(rustc_private)]
extern crate libc; to your example, and remove the libc dependency from your Cargo.toml, your code will also work, because it will use the Exploring a solution in this direction might be worth it. That is, you want a #![no_std] binary with a libc that does link the C standard library. It shouldn't be necessary for you to have to pull in |
@mati865 maybe we could move the parts of the We could then make This allows you to pull in different versions of the bindings, by pulling different versions of the libc crate, into a dependency graph, but ensuring that stuff is linked only once. |
Thanks for the explanation, that does make some sense. I've tried your suggestion with I've found an even simpler alternative to your previous workaround though: Just passing |
@gnzlbg I never played with To make sure I understand |
No, i think this just doesn't work. The
I think it can be an
This isn't what I meant. The I haven't worked out the rest. For example, if So maybe we could publish That means that just including |
I tried to build a simple
no_std
"Hello World!" program using thex86_64-unknown-linux-musl
target. The source code (insrc/main.rs
) is here:When using my default
x86_64-unknown-linux-gnu
target, the program compiles and runs like it should, but withx86_64-unknown-linux-musl
, I get this linking error:I can build a normal (
std
-using) "Hello World!" program for musl without problems, only thisno_std
version doesn't work.I am also able to work around this problem by instead moving the source code to
src/lib.rs
and compiling it as a library withcrate-type = ["staticlib"]
, then linking manually withmusl-gcc
. The error only occurs when trying to build a binary directly.My Cargo.toml. Uncomment the two lines and move main.rs to lib.rs for the workaround.
The text was updated successfully, but these errors were encountered: