How to build crate's dependencies as dylibs & link to them?

Hello. I'm concerned about Rust's slow compile times & big target directories. I'd like to do this: build all lib dependencies of some bin crate as dylib, put them in some system-wide directory, build this bin crate and finally link to those dylib's. It'll greatly speed up compile times as many Rust projects have big common dependencies like nalgebra, serde, syn & so on.

My question is, how to accomplish this with cargo? If this can't be done with plain cargo, which external crates should I use? If there aren't any existing solutions for my problem, then please give me a hint how to implement this myself? Maybe as a cargo plugin?

Also will my approach work for crates having lots of generics and even const generics, like nalgebra? I mean is it supposed to work across crate bounds with dynamic linker? AFAIK linkers don't know about generics at all and function names are mangled.

I'm aware that Rust has no stable ABI. But I would be happy to get it working at least using the same version of compiler for all crates.

I don't think this will give you improvements you hope for. Dependencies are already build as separate rlibs, and only recompiled if needed. Without LTO enabled there's no extra work done compared to using dynamic libraries.

And the crates that use proc macros, generics, and inlining can't meaningfully exist as dynamic libraries. The macros will be re-executed, and inlined/generic code will be "copy'n'pasted" and compiled in the target crate that uses them. That's unavoidable.

If you use Docker or something that blows away the target dir, then try changing your setup to preserve it. If you have many different Rust projects and you want to share build cache between them, then use sccache.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.