Repository navigation
Cannot link dylib crate to staticlib crate #118162
Description
Activity
- addedneeds-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triagingThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
on Nov 22, 2023 Transferred to rust-lang/rust since the compiler is responsible for this.
I'm not sure how possible this is to support.
Reacted by Rosalyn GreeneFwiw, the Rust Reference explicitly says this is not supported
If a static library is being produced, all upstream dependencies are required to be available in rlib formats. This requirement stems from the reason that a dynamic library cannot be converted into a static format.
Note that it is impossible to link in native dynamic dependencies to a static library, and in this case warnings will be printed about all unlinked native dynamic dependencies.However, it does not seem impossible to get this to work (possibly I'm missing something here)--I would certainally not expect rust to somehow turn a dynamic library into a static one as the reference indicates, but just supply linker flags for the downstream buildsystem to link to the required dynamic library.
It seems propagation could work the same way
rlibsare able to propagate#[link(name = "...")]attributes, which already supplies link flags throughcargo rustc -- --print=native-static-libsto get it added to the link. I think adding an absolute path to the.soinnative-static-libswould cover most usecases (lets downstream system decide how it will be finding the rust libraries at runtime). Not sure about non-Linux platforms.So, for the link-dylib-to-staticlib example, running
cargo rustc -- --print=native-static-libswould yield something likenote: native-static-libs: -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc path/to/link-dylib-to-staticlib/target/debug/libdy.soAh, this exact thing is implemented in #106560 under an unstable flag, very cool!
Not sure if this should be closed considering I don't see any issue tracking this feature...
Edit: should the docs be updated to reference the flag? Or is it undocumented on purpose?
- addedA-linkageArea: linking into static, shared libraries and binariesArea: linking into static, shared libraries and binariesA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.and removedneeds-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triagingThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
on Feb 18, 2024
Problem
If you have a
dylib-kind crate and try to use it from astaticlib-kind crate, you get this error:Steps
cargo buildPossible Solution(s)
Ideally metadata in the
.libwould be generated to make the eventual target link to the shared library, I assume through thenative-static-libs. It would be fine without this though, I'm fine with manually linking it.Alternatively, if this is not a valid configuration, a better error message would be appreciated
Notes
My usecase is something I think should be supported--I'm writing a static library to be consumed from a C++ application, and it needs a tokio runtime that will be shared with other static libraries. So I've put the tokio runtime in the dynamic library.
My workaround for now is to make it a
cdyliband not to mangle the function, and import it with anexternsection.Maybe there's a better way to do what I'm trying to do?
Version