Reduce binary size for embedded

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 !

1 Like

Rustlog : Why is a Rust executable large? covers most of your options, though switching to the global allocator has changed: GlobalAlloc in core::alloc - Rust

2 Likes

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..

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

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

3 Likes

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

Thanks a lot, this is perfect !

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.

6 Likes