Note: This is a reference question (but feel free to write answers of your own!)

I want to use the AVR tools directly -- no arduino-builder or arduino-cli. I would also like compilation and uploading to be as fast as reasonably possible

  • How fast? Can you put a metric on that? Uploading necessarily takes time, because it uses serial communications. Compiling, once invoked by the IDE is likely to be as fast as if you typed it on the command line. Of course, we would all like it to be faster than it currently is. But your requirement to be "fast" is rather un-specific.
    – Nick Gammon
    Commented Jul 4, 2022 at 10:05
  • Oh, I see this is a reference question. In that case see: arduino.stackexchange.com/questions/15893/…
    – Nick Gammon
    Commented Jul 4, 2022 at 10:08

1 Answer 1

  1. Create a new file called Makefile in your project directory. Populate it with the following contents:
TEMPDIR := $(shell mktemp -d)

    avr-g++ -DF_CPU=<CLK> -mmcu=<PARTNO> -fno-threadsafe-statics -O3 -flto -std=c++23 -isystem/usr/avr/include -lm -fuse-linker-plugin -Wl,--gc-sections *.cpp -o ${TEMPDIR}/a.elf

    avr-objcopy -O ihex -R .eeprom ${TEMPDIR}/a.elf ${TEMPDIR}/a.hex
    avrdude -V -p<PARTNO> -carduino -P/dev/tty<SERIAL_PORT> -b<BAUD> -D -Uflash:w:${TEMPDIR}/a.hex

    rm ${TEMPDIR}/a.elf ${TEMPDIR}/a.hex
    rm -d ${TEMPDIR}
  1. In the Arduino IDE, press Ctrl+, and enable verbose output during compilation and uploading.
  2. Start uploading with Ctrl+U. Replace the groups of angled brackets in Makefile (e.g. <CLK>) with the values in the Arduino IDE's build output.
  3. Run make.

-lm links in the AVR math library. *.cpp refers to your c++ source files. -V prevents avrdude from reading back the flash contents and verifying it, which saves time, but you may want to disable it while diagnosing problems.

  • 2
    For a similar but more general solution, you may want to take a look at Sudar Muthu's Makefile. If you are not using the Arduino core library, you can remove a lot of cruft from the g++ command line. Commented Jun 29, 2022 at 15:30
  • 1
    Checking the signature and verifying the flashed application are important safety features, I recommend to keep them to avoid long hours of searching errors. Commented Jun 30, 2022 at 9:09
  • 1
    @busybee Amended, thanks
    – glibg10b
    Commented Jun 30, 2022 at 9:48
  • @edgar I just realized "a lot of cruft" refers to my answer, and not Sudar Muthu's Makefile. Fixed, thanks :)
    – glibg10b
    Commented Jul 4, 2022 at 7:45
  • You could enhance your answer a lot, if you add explanations why each step is necessary, and what it contributes to the process. I'm missing for example that the intermediate step with the Arduino IDE is just for finding some decent values for clock and MCU, which could be entered as well manually, since you chose them in the IDE before. -- A great answer would also reason on each option of each tool. Especially as a beginner you need to learn what is important and what is not. Currently this is just one possible way thrown without additional information to unaware users. Commented Jul 5, 2022 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.