The optiboot source code blundled along the Arduino package is out of date; it means, it doesn’t compile with the newer GCC releases. I got this similar situation, while trying to build my own release as of march 2018 (AVR-GCC 4.9). As it was suggested in the post I got the latest release on github. And it worked.
After that I wanted to customize the optiboot for my own boards, in which the reset line isn’t driven by my home-made USB-TTL adapter (and other commercial adapters). So I made two changes to the project: I made the led to blink 5 times, and let the board to wait longer than a second before the bootloader resets itself (one second wasn’t enough to sync the PC and the board). For this the optiboot.c and Makefile files must be edited.
Changes to optiboot.c
Look for this line (as for this writing it’s the line 509):
I changed it to:
It gives me enough time to sync. From lines 319 to 330 you can see other options.
Changes to Makefile
Look for the line 165 and set the number of flashes that you want. I chose 5:
LED_START_FLASHES_CMD = -DLED_START_FLASHES=5
Building the solution
Open a console in the same location as the files and type:
make clean make atmega328
Depending on where you uncompressed or downloaded the Optiboot github project this can be the last step, but not for me.
Copying the optiboot_atmega329.hex file
In order not to break things, I decompressed the project in a new folder called Optiboot-master. So after the solution was build, I copied such a file into the original optiboot folder so the IDE is able to find it whenever I upload the bootloader to the chip. A softlink seems to be a better workaround.
I tested all this mess using a USBAsp V2.0 programmer and a home-made chip place-holder, as shown:
Open a terminal in ~/arduino-1.8.5/hardware/arduino/avr/bootloaders/optiboot and type:
$ avrdude -p m328p -P usb -c USBasp -e -U flash:w:optiboot_atmega328.hex -Ulock:w:0x3F:m -Uefuse:w:0xfd:m -Uhfuse:w:0xde:m -Ulfuse:w:0xff:m -C/usr/share/arduino/hardware/tools/avrdude.conf
and an output’s extract is shown as well: