Skip to content
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

Cannot link dylib crate to staticlib crate #118162

Open
russelltg opened this issue Nov 11, 2023 · 3 comments
Open

Cannot link dylib crate to staticlib crate #118162

russelltg opened this issue Nov 11, 2023 · 3 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@russelltg
Copy link

russelltg commented Nov 11, 2023

Problem

If you have a dylib-kind crate and try to use it from a staticlib-kind crate, you get this error:

error: crate `dylib` required to be available in rlib format, but was not found in this form

Steps

  1. Checkout https://github.com/russelltg/link-dylib-to-staticlib
  2. cargo build

Possible Solution(s)

Ideally metadata in the .lib would be generated to make the eventual target link to the shared library, I assume through the native-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 cdylib and not to mangle the function, and import it with an extern section.

Maybe there's a better way to do what I'm trying to do?

Version

cargo 1.73.0 (9c4383fb5 2023-08-26)
release: 1.73.0
commit-hash: 9c4383fb55986096b414d98125421ab87b5fd642
commit-date: 2023-08-26
host: x86_64-pc-windows-msvc
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.2.1-DEV (sys:0.4.65+curl-8.2.1 vendored ssl:Schannel)
os: Windows 10.0.19045 (Windows 10 Pro) [64-bit]
@russelltg russelltg added the C-bug Category: This is a bug. label Nov 11, 2023
@ehuss ehuss transferred this issue from rust-lang/cargo Nov 22, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 22, 2023
@ehuss
Copy link
Contributor

ehuss commented 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.

@russelltg
Copy link
Author

Fwiw, 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 rlibs are able to propagate #[link(name = "...")] attributes, which already supplies link flags through cargo rustc -- --print=native-static-libs to get it added to the link. I think adding an absolute path to the .so in native-static-libs would 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-libs would yield something like

note: native-static-libs: -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc path/to/link-dylib-to-staticlib/target/debug/libdy.so

@russelltg
Copy link
Author

russelltg commented Nov 25, 2023

Ah, 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?

@jieyouxu jieyouxu added A-linkage Area: linking into static, shared libraries and binaries A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants