# Build-time detection of linked libraries

**URL:** <https://internals.rust-lang.org/t/build-time-detection-of-linked-libraries/17747>\
**Category:** cargo\
**Created:** [November 15, 2022, 6:59am UTC](https://internals.rust-lang.org/t/build-time-detection-of-linked-libraries/17747 "2022-11-15T06:59:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tari](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tari/32/1415_2.png) [@tari](https://internals.rust-lang.org/u/tari)\
**Post date:** [November 15, 2022, 6:59am UTC](https://internals.rust-lang.org/t/build-time-detection-of-linked-libraries/17747/1 "2022-11-15T06:59:55Z")

</div>

[A recent rustc linking change](https://github.com/rust-lang/rust/pull/102832) has exposed that some crates link to libraries that can't be put in the crate's manifest `links` because the linked libraries aren't known until build-time, and that can cause build errors when other crates attempt to link against the same library.

[In the case of `llvm-sys`](https://gitlab.com/taricorp/llvm-sys.rs/-/issues/41), `libffi` is sometimes pulled in because LLVM can be configured to use libffi itself, but this is impossible to know until build time since it's an optional dependency of LLVM. I imagine there may be other C library bindings that may exhibit similar behavior, but am not aware of any right now.

It seems like handling this correctly would require allowing build scripts to emit a build-time equivalent of the `links` field in the crate manifest: is that a feasible thing to support? Or is there an alternative way to handle this sort of situation that I'm missing?

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [November 15, 2022, 7:34am UTC](https://internals.rust-lang.org/t/build-time-detection-of-linked-libraries/17747/2 "2022-11-15T07:34:27Z")

</div>

Isn’t it fine to have multiple crates emit `rustc-link-lib` for the same library? AFAIK the `links` key is just a mitigation against having multiple `-sys` libraries attempting to manage the global state of the C library. (So not having this feature allows builds to succeed and run into runtime issues, not fail to compile).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 22, 2024, 5:23pm UTC](https://internals.rust-lang.org/t/build-time-detection-of-linked-libraries/17747/3 "2024-12-22T17:23:11Z")

</div>

Closed on December 22, 2024, 5:23pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
