rust-lang / rust-lang/rust

Cannot link `dylib` crate to `staticlib` crate

Open
#118,162 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs A-linkage C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by checking out the linked reproducer and running cargo build to confirm the dylib-to-staticlib failure. Then trace the Rust compiler and linker handling for crate formats and native-static-libs; done means either the configuration links successfully with appropriate metadata or the diagnostic clearly explains that it is unsupported.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.