Reduce binary size for embedded

#1

Hello there.
I juste compiled the latest blue-pill demo and the file is more than 1.3 Mb while the microcontroller only has 64k of memory…

git clone https://github.com/lupyuen/stm32-blue-pill-rust
cd stm32-blue-pill-rust
cargo build 
ll target/thumbv7m-none-eabi/release
141Ki stm32-blue-pill-rust

How do I get to reduce it’s size ? I’d like to keep my debug symbols (so stripping all symbols is not a very good idea…).

Thanks !

#2

https://lifthrasiir.github.io/rustlog/why-is-a-rust-executable-large.html covers most of your options, though switching to the global allocator has changed: https://doc.rust-lang.org/stable/core/alloc/trait.GlobalAlloc.html

1 Like
#3

Thanks a lot for your reply !
This is for embedded development, so there is no linking to external libraries, no allocation, etc.
I think the only valuable advice is to strip the executable but I do still want to be able to debug it…

#4

I’m actually not sure that rustc will remove the allocator if you don’t use allocation. You should check.

#5

@gbip Please check the FAQ. There’s an entry for this exact question.

2 Likes
#6

Another note: the s and z optimization levels stabilized in 1.28.0.

#7

Thanks a lot, this is perfect !

#8

For future reference, I’ve created a min-sized-rust repository that details all of the ways (that I’m aware of) to minimize the binary size of Rust programs.

4 Likes