Atmega328p build fails: I found the way!

tl;dr = use avr-gcc 7.3.0, and disable some functions.
download, unpack, and add ‘(unpacked avr-gcc path)/bin‘ to PATH environment variables.
(example: add ‘/opt/avr-gcc-7.3.0-x64-linux/bin’ to PATH)

make menuconfig → Enable extra low-level configuration options → Optional features (to reduce code size) → turn off unused features.

! I did not test this on windows !
I tested on Debian 12 bookworm (amd64)


When you build klipper, you commonly use os default avr-gcc.

but there’s issue. some avr-gcc occurs ‘out of size’ error when building klipper for 328p. there’s many error reports!

# avr-gcc --version
avr-gcc (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# make 
  Creating symbolic link out/board
  Building out/autoconf.h
  Compiling out/src/sched.o
  Compiling out/src/command.o
  Compiling out/src/basecmd.o
  Compiling out/src/debugcmds.o
  Compiling out/src/initial_pins.o
  Compiling out/src/gpiocmds.o
  Compiling out/src/stepper.o
  Compiling out/src/endstop.o
  Compiling out/src/trsync.o
  Compiling out/src/adccmds.o
  Compiling out/src/spicmds.o
  Compiling out/src/i2ccmds.o
  Compiling out/src/pwmcmds.o
  Compiling out/src/buttons.o
  Compiling out/src/tmcuart.o
  Compiling out/src/neopixel.o
  Compiling out/src/pulse_counter.o
  Compiling out/src/lcd_st7920.o
  Compiling out/src/lcd_hd44780.o
  Compiling out/src/spi_software.o
  Compiling out/src/i2c_software.o
  Compiling out/src/thermocouple.o
  Compiling out/src/sensor_adxl345.o
  Compiling out/src/sensor_lis2dw.o
  Compiling out/src/sensor_mpu9250.o
  Compiling out/src/sensor_icm20948.o
  Compiling out/src/sensor_hx71x.o
  Compiling out/src/sensor_ads1220.o
  Compiling out/src/sensor_ldc1612.o
  Compiling out/src/sensor_angle.o
  Compiling out/src/sensor_bulk.o
  Compiling out/src/sos_filter.o
  Compiling out/src/load_cell_probe.o
  Compiling out/src/avr/main.o
  Compiling out/src/avr/timer.o
  Compiling out/src/avr/gpio.o
  Compiling out/src/avr/adc.o
  Compiling out/src/avr/spi.o
  Compiling out/src/avr/i2c.o
  Compiling out/src/avr/hard_pwm.o
  Compiling out/src/avr/watchdog.o
  Compiling out/src/avr/serial.o
  Compiling out/src/generic/serial_irq.o
  Building out/compile_time_request.o
Version: v0.13.0-425-g867d73f0b
  Linking out/klipper.elf
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: out/klipper.elf section `.text' will not fit in region `text'
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800c54 of out/klipper.elf section `.data' is not within region `data'
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800dc5 of out/klipper.elf section `.bss' is not within region `data'
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800c54 of out/klipper.elf section `.data' is not within region `data'
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800dc5 of out/klipper.elf section `.bss' is not within region `data'
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: region `text' overflowed by 12464 bytes
collect2: error: ld returned 1 exit status
make: *** [Makefile:73: out/klipper.elf] Error 1

Thankfully, ZakKemble make a standalone avr-gcc builds!

I tried many versions… and get fails… :innocent:

15.2.0, 14.1.0 → most recent version, prior version. does not work.

# avr-gcc --version
avr-gcc (GCC) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# make 
  Creating symbolic link out/board
  Building out/autoconf.h
  Compiling out/src/sched.o
  Compiling out/src/command.o
  Compiling out/src/basecmd.o
  Compiling out/src/debugcmds.o
  Compiling out/src/initial_pins.o
  Compiling out/src/gpiocmds.o
  Compiling out/src/stepper.o
  Compiling out/src/endstop.o
  Compiling out/src/trsync.o
  Compiling out/src/adccmds.o
  Compiling out/src/spicmds.o
  Compiling out/src/i2ccmds.o
  Compiling out/src/pwmcmds.o
  Compiling out/src/buttons.o
  Compiling out/src/tmcuart.o
  Compiling out/src/neopixel.o
  Compiling out/src/pulse_counter.o
  Compiling out/src/lcd_st7920.o
  Compiling out/src/lcd_hd44780.o
  Compiling out/src/spi_software.o
  Compiling out/src/i2c_software.o
  Compiling out/src/thermocouple.o
  Compiling out/src/sensor_adxl345.o
  Compiling out/src/sensor_lis2dw.o
  Compiling out/src/sensor_mpu9250.o
  Compiling out/src/sensor_icm20948.o
  Compiling out/src/sensor_hx71x.o
  Compiling out/src/sensor_ads1220.o
  Compiling out/src/sensor_ldc1612.o
  Compiling out/src/sensor_angle.o
  Compiling out/src/sensor_bulk.o
  Compiling out/src/sos_filter.o
  Compiling out/src/load_cell_probe.o
  Compiling out/src/avr/main.o
  Compiling out/src/avr/timer.o
  Compiling out/src/avr/gpio.o
  Compiling out/src/avr/adc.o
  Compiling out/src/avr/spi.o
  Compiling out/src/avr/i2c.o
  Compiling out/src/avr/hard_pwm.o
  Compiling out/src/avr/watchdog.o
  Compiling out/src/avr/serial.o
  Compiling out/src/generic/serial_irq.o
  Building out/compile_time_request.o
Version: v0.13.0-425-g867d73f0b
  Linking out/klipper.elf
/opt/avr-gcc/bin/../lib/gcc/avr/15.2.0/../../../../avr/bin/ld: out/klipper.elf section `.text' will not fit in region `text'
/opt/avr-gcc/bin/../lib/gcc/avr/15.2.0/../../../../avr/bin/ld: address 0x800c4e of out/klipper.elf section `.data' is not within region `data'
/opt/avr-gcc/bin/../lib/gcc/avr/15.2.0/../../../../avr/bin/ld: address 0x800dbf of out/klipper.elf section `.bss' is not within region `data'
/opt/avr-gcc/bin/../lib/gcc/avr/15.2.0/../../../../avr/bin/ld: address 0x800c4e of out/klipper.elf section `.data' is not within region `data'
/opt/avr-gcc/bin/../lib/gcc/avr/15.2.0/../../../../avr/bin/ld: address 0x800dbf of out/klipper.elf section `.bss' is not within region `data'
/opt/avr-gcc/bin/../lib/gcc/avr/15.2.0/../../../../avr/bin/ld: region `text' overflowed by 13294 bytes
collect2: error: ld returned 1 exit status
make: *** [Makefile:73: out/klipper.elf] Error 1

13.2.0 → got a build success, but this occurs TON of warnings!

# avr-gcc --version
avr-gcc (GCC) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# make
# --- omitted ---
In function 'console_task':
cc1: note: source object is likely at address zero
In function 'irq_restore',
    inlined from 'console_pop_input' at src/generic/serial_irq.c:68:9,
    inlined from 'console_task' at src/generic/serial_irq.c:85:9:
out/board/irq.h:28:10: warning: array subscript 0 is outside array bounds of 'volatile uint8_t[0:]' [-Warray-bounds=]
   28 |     SREG = flag;
      |          ^
In function 'console_task':
cc1: note: source object is likely at address zero
In function 'irq_restore',
    inlined from 'console_pop_input' at src/generic/serial_irq.c:64:13,
    inlined from 'console_task' at src/generic/serial_irq.c:85:9:
out/board/irq.h:28:10: warning: array subscript 0 is outside array bounds of 'volatile uint8_t[0:]' [-Warray-bounds=]
   28 |     SREG = flag;
      |          ^
In function 'console_task':
cc1: note: source object is likely at address zero
  Building out/compile_time_request.o
Version: v0.13.0-425-g867d73f0b
  Linking out/klipper.elf
  Creating hex file out/klipper.elf.hex

12.1.0 → also prints warnings, but less than 13.2.0!

# avr-gcc --version
avr-gcc (GCC) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# make
# --- omitted ---
  Compiling out/src/generic/serial_irq.o
In file included from src/generic/serial_irq.c:10:
In function 'irq_save',
    inlined from 'console_pop_input' at src/generic/serial_irq.c:61:28,
    inlined from 'console_task' at src/generic/serial_irq.c:85:9:
out/board/irq.h:21:13: warning: array subscript 0 is outside array bounds of 'volatile uint8_t[0:]' [-Warray-bounds]
   21 |     uint8_t flag = SREG;
      |             ^~~~
In function 'irq_restore',
    inlined from 'console_pop_input' at src/generic/serial_irq.c:68:9,
    inlined from 'console_task' at src/generic/serial_irq.c:85:9:
out/board/irq.h:28:10: warning: array subscript 0 is outside array bounds of 'volatile uint8_t[0:]' [-Warray-bounds]
   28 |     SREG = flag;
      |          ^
In function 'irq_restore',
    inlined from 'console_pop_input' at src/generic/serial_irq.c:64:13,
    inlined from 'console_task' at src/generic/serial_irq.c:85:9:
out/board/irq.h:28:10: warning: array subscript 0 is outside array bounds of 'volatile uint8_t[0:]' [-Warray-bounds]
   28 |     SREG = flag;
      |          ^
  Building out/compile_time_request.o
Version: v0.13.0-425-g867d73f0b
  Linking out/klipper.elf
  Creating hex file out/klipper.elf.hex

11.1.0 → finally, it seems totally okay!

# avr-gcc --version
avr-gcc (GCC) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# make clean
  Creating symbolic link out/board
  Building out/autoconf.h
  Compiling out/src/sched.o
  Compiling out/src/command.o
  Compiling out/src/basecmd.o
  Compiling out/src/debugcmds.o
  Compiling out/src/initial_pins.o
  Compiling out/src/gpiocmds.o
  Compiling out/src/stepper.o
  Compiling out/src/endstop.o
  Compiling out/src/trsync.o
  Compiling out/src/adccmds.o
  Compiling out/src/spicmds.o
  Compiling out/src/i2ccmds.o
  Compiling out/src/pwmcmds.o
  Compiling out/src/buttons.o
  Compiling out/src/tmcuart.o
  Compiling out/src/neopixel.o
  Compiling out/src/pulse_counter.o
  Compiling out/src/lcd_st7920.o
  Compiling out/src/lcd_hd44780.o
  Compiling out/src/spi_software.o
  Compiling out/src/i2c_software.o
  Compiling out/src/thermocouple.o
  Compiling out/src/sensor_adxl345.o
  Compiling out/src/sensor_lis2dw.o
  Compiling out/src/sensor_mpu9250.o
  Compiling out/src/sensor_icm20948.o
  Compiling out/src/sensor_hx71x.o
  Compiling out/src/sensor_ads1220.o
  Compiling out/src/sensor_ldc1612.o
  Compiling out/src/sensor_angle.o
  Compiling out/src/sensor_bulk.o
  Compiling out/src/sos_filter.o
  Compiling out/src/load_cell_probe.o
  Compiling out/src/avr/main.o
  Compiling out/src/avr/timer.o
  Compiling out/src/avr/gpio.o
  Compiling out/src/avr/adc.o
  Compiling out/src/avr/spi.o
  Compiling out/src/avr/i2c.o
  Compiling out/src/avr/hard_pwm.o
  Compiling out/src/avr/watchdog.o
  Compiling out/src/avr/serial.o
  Compiling out/src/generic/serial_irq.o
  Building out/compile_time_request.o
Version: v0.13.0-425-g867d73f0b
  Linking out/klipper.elf
  Creating hex file out/klipper.elf.hex

so… I got a firmware binary, but i still did not test it… my printer is still build in process. :thinking:
If you have 328p based board and want to use it for expand klipper, try this!


EDIT: build is okay, but still firmware are too big when enable all features.
atmega328p have 32Kb flash, and bootloader takes some space. (512 byte?)

but built firmware size is over 32Kb, so it can be flashed…

## avr-gcc 11.1.0
# avr-objcopy -I ihex -O binary out/klipper.elf.hex out/klipper.bin && ls -alh out/klipper.bin
-rw-r--r-- 1 debian users 39K Dec 19 01:37 out/klipper.bin

## avr-gcc 7.3.0
# avr-objcopy -I ihex -O binary out/klipper.elf.hex out/klipper.bin && ls -alh out/klipper.bin
-rw-r--r--  1 debian users  36K Dec 19 01:28 out/klipper.bin

# avrdude -p m328p -c arduino -P /dev/ttyUSB1 -b 57600 -U flash:w:out/klipper.elf.hex:i

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude error: address 0x8010 out of range at line 2049 of out/klipper.elf.hex
avrdude error: read from file out/klipper.elf.hex failed

avrdude done.  Thank you.

after disable some features (in my case, i disable some sensors) size hugly reduced and it flashed successfully.

## After disable some features
# make
  Creating symbolic link out/board
  Building out/autoconf.h
  Compiling out/src/sched.o
  Compiling out/src/command.o
  Compiling out/src/basecmd.o
  Compiling out/src/debugcmds.o
  Compiling out/src/initial_pins.o
  Compiling out/src/gpiocmds.o
  Compiling out/src/stepper.o
  Compiling out/src/endstop.o
  Compiling out/src/trsync.o
  Compiling out/src/adccmds.o
  Compiling out/src/spicmds.o
  Compiling out/src/i2ccmds.o
  Compiling out/src/pwmcmds.o
  Compiling out/src/buttons.o
  Compiling out/src/tmcuart.o
  Compiling out/src/neopixel.o
  Compiling out/src/pulse_counter.o
  Compiling out/src/lcd_st7920.o
  Compiling out/src/lcd_hd44780.o
  Compiling out/src/spi_software.o
  Compiling out/src/i2c_software.o
  Compiling out/src/avr/main.o
  Compiling out/src/avr/timer.o
  Compiling out/src/avr/gpio.o
  Compiling out/src/avr/adc.o
  Compiling out/src/avr/spi.o
  Compiling out/src/avr/i2c.o
  Compiling out/src/avr/hard_pwm.o
  Compiling out/src/avr/watchdog.o
  Compiling out/src/avr/serial.o
  Compiling out/src/generic/serial_irq.o
  Building out/compile_time_request.o
Version: v0.13.0-428-ga8cbc9355
  Linking out/klipper.elf
  Creating hex file out/klipper.elf.hex

# avr-gcc 11.1.0
# avr-objcopy -I ihex -O binary out/klipper.elf.hex out/klipper.bin && ls -alh out/klipper.bin
-rw-r--r-- 1 ubuntu users 26K Dec 19 01:38 out/klipper.bin

## avr-gcc 7.3.0
# avr-objcopy -I ihex -O binary out/klipper.elf.hex out/klipper.bin && ls -alh out/klipper.bin
-rw-r--r--  1 debian users  24K Dec 19 01:32 out/klipper.bin
# avrdude -p m328p -c arduino -P /dev/ttyUSB1 -b 57600 -U flash:w:out/klipper.elf.hex:i

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file out/klipper.elf.hex for flash
         with 23642 bytes in 1 section within [0, 0x5c59]
         using 185 pages and 38 pad bytes
avrdude: writing 23642 bytes flash ...

Writing | ################################################## | 100% 6.57 s 

avrdude: 23642 bytes of flash written
avrdude: verifying flash memory against out/klipper.elf.hex

Reading | ################################################## | 100% 5.40 s 

avrdude: 23642 bytes of flash verified

avrdude done.  Thank you.