(C++) How do I reduce binary size?

Hey folks,

I’ve recently finished a project and I’ve run into an issue: I’m almost out of flash space for my binary. It currently uses 99.88% of the allotted 128kb flash.

I’d like to add quite a few more features to my project but at this rate I won’t be able to add much more before I run out of flash.

I’m aware that there are plans to increase flash memory in the futue but it’s not known when this will be complete and it looks like it may come with some performance caveats.

  1. Why was the decision made to have only 128kb flash? I’m new to hardware and I don’t know how much flash room is an adequate amount but it seems like the 128kb limit has been an issue for myself and a few others on the forum.
  2. How can I go about reducing the binary size of my program, so that I can keep adding features? Since this is my first program for embedded, I’m not familiar with common design patterns etc. - I’ve used a pretty standard OOP design pattern but I don’t know if that’s correct for a platform like daisy.
  3. I currently use a few things from std; std::string, std::vector, std::array, and some others. Is it adviseable to use these? I imagine it would save some space to use more elementary C functionalities instead. It would sure be a pain in the ass to give up the conveniences that these libraries offer me, though.
  4. I use the included libDaisy makefile to build my project, just like everything else in daisy-examples does. Are the compiler flags in this makefile optimized? I know that certain compiler flags can be added to reduce binary size.
  5. Could I use something like UPX to compress the binary?

The source code is here, if you’d like to take a look.
Thank you for your help :slight_smile:

Big thank you to @antisvin for his advice the the other thread:

You can add OPT = -Os to your Makefile anywhere above the include $(SYSTEM_FILES_DIR)/Makefile line to compile with optimization targeted to reduce code size. Sometimes that may break things, but looks like your patch builds and runs fine and you get ~15kb free.

This reduced my binary size by almost 10%.

1 Like