How can I compile my code via terminal? I am working on a Chrome app that allows you to write, compile, and upload code to an Arduino, and I need to know this so I can use the command line compiler on my Linux server. Mac or Linux terminal, it doesn't matter.
3 Answers
Check out platformio.org it's the best solution out there for command line compiling.
-
See my answer to a related question for an example PlatformIO CLI session. Commented Dec 31, 2021 at 21:46
Assuming an AVR-based Arduino, you would need the avr-gcc compiler, the avr-libc and the Arduino libraries. Also avrdude if you want to upload. The only issue is that the commands used for the build process are quite long (lots of options). You can see them by enabling verbose compilation in the IDE. Thus you will probably want some way to automate the whole thing.
There are several options for this. The most straightforward seems to be the arduino-builder command-line tool. This is part of the Arduino project and should ensure maximum compatibility. Otherwise you could use a plain old Makefile. I personally use Sudar Muthu' Makefile. It can be installed on a Debian-based Linux distribution by:
sudo apt install arduino-mk
It is not fully compatible with the classic Arduino build process
though: it does automatically include <Arduino.h>
into your sketch,
but it does not add forward declarations. Some prefer it this way, as
the automatic inclusion of forward declarations is somewhat buggy and
does not play well with some C++ features.
There is an Arduino CLI (command line interface):
https://github.com/arduino/arduino-cli
You use commands such as to access Arduino IDE functions:
arduino-cli compile --fqbn arduino:avr:leonardo sketch
and
arduino-cli upload -pCOM14 --fqbn arduino:avr:leonardo sketch
(obviously port will be different under Linux)
avr-gcc
.