-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
NetBSD error linking against C library with link dependency #96328
Comments
fn main() {
let mut builder = cc::Build::new();
builder.file("foo.c");
builder.compile("foo");
println!("cargo:rustc-link-lib=z");
} should work. |
@petrochenkov yes, that was exactly the issue! It seems that the build script prints:
which are "greedily" expanded into linker arguments. I (incorrectly) assumed that In this case, the original code compiled/linked fine on Linux, FreeBSD, and OpenBSD but failed on NetBSD. However, With that said, it probably would make sense to at least document the "order preserving behavior" of cargo build scripts and Thoughts? Does anyone think a behavior change makes sense as opposed to a pure documentation change? |
Since the order of libraries is significant, rustc tries to preserve it. Cargo also preserves the order, from my experience, but it's better to ask cargo maintainers whether are any corner cases. |
I wouldn't want to specify precise details for rustc, due to those legacy issues and because in rustc we want to deduplicate the linked native libraries more aggressively. But in simple cases like println!("cargo:rustc-link-lib=a");
println!("cargo:rustc-link-lib=b");
println!("cargo:rustc-link-lib=c"); (no duplicates or anything), it's obvious that we'll need to infer dependencies as |
doc: discuss build script instruction order ### What does this PR try to resolve? It is currently not documented that the order of build script `cargo:` instructions may be relevant to linking. This has caused issues such as: rust-lang/rust#96328 ### How should we test and review this PR? Build/view documentation. ### Additional information - Cargo maintainers should fact check my wording. - We may need to discuss if this should also be documented for `rustc` - Maintainers should ensure that this change does not prevent a change in what is currently unspecified behavior. Perhaps `cargo` will want to rearrange link arguments itself to resolve issues in the future?
Triage:
No objections were raised, so let's close this issue now. |
I tried this code:
Repo: https://github.com/tmfink/rust-link-issue
build.rs
:src/main.rs
:foo.c
:I expected this to compile, but got a link error:.
Meta
rustc --version --verbose
:Root Cause
The issue is caused by the link arg
-lz
not appearing at the end.Workarounds
Use a link wrapper script
You can use a wrapper script that copies the
-l*
arguments to the end:rust_link_fix.sh
:.cargo/config
:Reference the symbol in the Rust code
You can "trick" the linker into pulling in the library by referencing a symbol from that library in the Rust code:
https://github.com/tmfink/rust-link-issue/blob/c82d426559c4cf4c9a2537d52d7643b9c59b0639/src/main.rs#L16-L17
The text was updated successfully, but these errors were encountered: