Skip to content

Confusing suggestion to use cargo:rustc-link-lib in a cargo project with a lib and bins #129904

Open
@kusnezoff-alexander

Description

@kusnezoff-alexander

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:

  1. Without the existence of lib.rs: Linking to custom C-library does work (see ./linking-without-lib-works for working example)
  2. Withthe existence of lib.rs: Linking to custom C-library does not work (see ./linking-with-lib-doesnt for working example)

Steps

  1. git clone https://github.com/kusnezoff-alexander/cargo-build-linking-bug
  2. cd libs && make all
  3. cd linking-does-work && cargo build - should exit successfully
  4. cd 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

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-linkageArea: linking into static, shared libraries and binariesD-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions