Avr code size optimization revisited

Code size optimization is important. Especially on microcontrollers with their strict memory limitations. In my last post I wrote about optimization methods for WinAvr and GCC.

With the new release of WinAvr WinAvr 20070525 now includes GCC 4.x and an updated avr-libc 1.4.6. Therefore most of the steps described in my previous post are no longer required.

Simply adding:

  • CFLAGS += -ffunction-sections
  • LDFLAGS += -Wl,–gc-sections

to your custom makefile (no idea how to integrate this into a managed make project) will analyze your code. Methods that are not used in the project are not linked into the final binary.

I like to have versatile libraries for peripherals or other taks with many convenience methods for comfortable coding. Yet, normally only a rather small selection of these methods is used. With the linker and compiler options from above a significant reduction in byte size is achieved. In one project the binary size dropped by about 30 – 40 %.