Build small libstd for dynamic linking

In order to get more opportunity to use Rust at work. I'm looking at how to get rust with std to fit into a Linux Arm device (bigger than most embedded systems) with limited spare flash storage.

I've figured out a bunch of options already from GitHub - johnthagen/min-sized-rust: 🦀 How to minimize Rust binary size 📦 but one last one I'm looking at is using libstd dynamically (I'm aware that requires all code use the exact same compiler version). The default is a bit too large even stripped. I would like to see how big it is when compiled size optimized and with panic=abort. I've gotten build-std to work for static linking, but I couldn't get it to work with dynamic. I suspect there's some other way to compile a dynamic libstd.

If anyone has another suggestions on how to compile libstd smaller that would help as would any tips not covered by the min-sized-rust link.


I am very interested in this as well.

Can you give us some numbers for what is an "acceptable" size for your device? Most statically compiled Rust binaries I've seen are only a couple MB after you strip debug symbols and LTO does its thing.

Making space involves work shrinking/removing other software, so it's a trade off. For a single Rust process I would just use build_std and static linking, but this would help set a cap on lots of Rust programs. The system currently mostly C++ and dynamically links the C++ stdlib. So If I can limit the redundant overhead of Rust (assuming stdlibs do the same thing which isn't true but close enough), that might help get permissions to make some proof of concepts and figure out the real cost. I would guess 2-3MB might be doable, but I haven't gotten a number to aim for either.

You could save maybe a couple of percent on using panic=abort and -Copt-level=s/z, but most of the size comes from the fact that every public non-generic and non-inline function in the entire standard library has to be kept as a downstream user may use it. When statically linking this is not an issue as the linker is free to remove dead code and is in fact explicitly instructed to do so by rustc.

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.