Open
Description
Problem
-- I created this repo to showcase/reproduce the error: https://github.com/kusnezoff-alexander/cargo-build-linking-bug --
The success of linking C-libraries during cargo build
seems to depend on the existence of lib.rs
even if only the binary is built.
Building packages which offer both a library and binary could suffer from this potential bug. This bug has been only been reproduced so far for linking against a library via
println!("cargo:rustc-link-search=../libs");
println!("cargo:rustc-link-lib=static=mylib");
inside build.rs
.
Exptected behavior: The existence of a file called lib.rs
should have no effect on the linking during cargo build
:
Observed behavior:
- Without the existence of
lib.rs
: Linking to custom C-library does work (see./linking-without-lib-works
for working example) - Withthe existence of
lib.rs
: Linking to custom C-library does not work (see./linking-with-lib-doesnt
for working example)
Steps
git clone https://github.com/kusnezoff-alexander/cargo-build-linking-bug
cd libs && make all
cd linking-does-work && cargo build
- should exit successfullycd linking-doesnt-work && cargo build
- should throw an error that extern function-symbol isn't defined (although the only difference to the previous case is the existence of lib.rs)
Possible Solution(s)
No response
Notes
- interestlingly though, it does work if
export RUSTFLAGS="-L <absolute-path-to-this-dir>/libs/ -l mylib"
is set
Version
cargo 1.80.1 (376290515 2024-07-16)
release: 1.80.1
commit-hash: 37629051518c3df9ac2c1744589362a02ecafa99
commit-date: 2024-07-16
host: x86_64-unknown-linux-gnu
libgit2: 1.7.2 (sys:0.18.3 vendored)
libcurl: 8.6.0-DEV (sys:0.4.72+curl-8.6.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w 11 Sep 2023
os: Fedora 40.0.0 [64-bit]
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: linking into static, shared libraries and binariesDiagnostics: Confusing error or lint that should be reworked.Diagnostics: A diagnostic that is giving misleading or incorrect information.Relevant to the cargo team, which will review and decide on the PR/issue.