Skip to content

Allow linking against dylibs in LTO mode #31854

Open
@comex

Description

@comex

I would like to build a binary that links against some crates using LTO and others dynamically. (Why? Because I have a dynamic library that contains all of LLVM and is 30MB, slowing down linking. Note that this is not LLVM's own dynamic library, it's a Rust crate compiled to dylib that itself links LLVM statically. One might imagine more generic use cases, e.g. LTOing small dependencies and dynamically linking large ones.) Here's a model:

xa.rs:

#![crate_type = "bin"]
extern crate xb;
extern crate xc;
pub fn main() { xb::b(); xc::c(); }

xb.rs:

#![crate_type = "rlib"]
pub fn b() {}

xc.rs:

#![crate_type = "dylib"]
pub fn c() {}

This can be built successfully without LTO, albeit only if the dylib links libstd dynamically (otherwise you get "cannot satisfy dependencies so std only shows up once"):

rustc ../xc.rs -C prefer-dynamic -L. && rustc ../xb.rs -L. && rustc ../xa.rs -L.

The binary xa contains the code from xa.rs and xb.rs linked statically (but with a conventional linker rather than LTO), and dynamically links against libxc.dylib and libstd.

But if the last command includes -C lto, I just get:

error: could not find rlib for: `xc`

It would be nice if rustc supported this.

In principle support could also be added for combining LTO and non-LTO static linking, but that would require some method to identify which is desired, since both types refer to .rlib files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    -Cprefer-dynamicCodegen option: Prefer dynamic linking to static linking.A-linkageArea: linking into static, shared libraries and binariesC-feature-requestCategory: A feature request, i.e: not implemented / a PR.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions