Showing results for 
Search instead for 
Did you mean: 

How to reduce the size of build file.

Associate III


I was trying to convert a code written in HAL to baremetal for reducing the size. The code in HAL has 90KB file size when build in Debug mode and has 50KB file size in Release mode. In case of baremetal 73KB and 63KB respectively for Debug and Release modes. How can I reduce the file size of baremetal code than the HAL Release mode.


Accepted Solutions

Perhaps review .MAP​ and .LST​, look at what's eating the most memory.

If there's repetitive stuff that could be a subroutine or otherwise factored.

Compilers are not good with intent or identifying algorithms.

A lot of register code is very inefficient, using a lot of individual RMW code that the compiler can't fold due to the volatile and in-order expectations constraining it. Understand the load-store architecture and use your overview to decide what can be folded and where the order is or is not critical. ​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

Senior III

Try to set compiler optimization to "Optimize for size"

Associate III

Compiler optimization is already in Optimize for size

Senior III

It is hard to say. If the bare metal code is larger than the HAL, then the code is inefficient. Try using the LL code instead. If that doesn't help either, the easiest way is to use an MCU with more flash memory. The last option is to limit the scope (and functionality) of the code.

Associate III

If u r using custom linker script, first you must check it.

Not using custom linker script

But in Debug mode I got less build size. Could you please tell me what will be causing the size variation.


Can we be precise in the question please ?

Bare Metal xor RTOS based code. It has nothing to relate to LL or HAL.

Second, when you build and you look at the code size, does the code WORK ?

Debug and Release is just an artifact to have 2 set of project compiler properties. It's about compiler optimisation levels, linker file settings, etc...

The non HAL LL encoded application resulting memory footprint will mostly depend on the coder's skill and write C "ASM in mind". So in short, if the code sizes don't make sense, it's probably because something is not mastered.

yes the code works fine in alll cases. So could you please give me some suggestions improve the code size.


There are many options.

  1. Configure the compiler to optimize its output for size.
  2. Remove library code that's linked that you don't need.
  3. Disable options you don't really require. E.g. Cube defaults are geared towards beginner developers.
  4. Re-write your code with greater skill.
  5. Re-write parts of your standard library code, tailoring to your requirements.
  6. Replace HAL or parts of it with LL.
  7. Remove RTOS.
  8. Use a different compiler, e.g. last I checked, IAR's output was smaller than GCC for comparable optimization.

Your link map would tell you how much size you need to reduce and indicate some places you might reduce the size most easily.