Arduino-CLI, how to compile just one file?

Is there any way to just compile one file by itself? This is something I do regularly when I am developing with gcc-based projects. I rarely use either 1.x or 2.x IDE, just using Arduino-CLI (there doesn't seem to be a forum category for this under the development tools section).

When I tried to just add single file to the CLI command, it errors out because it thinks the name I gave is an INO project file.

That's odd because I just moved this topic to the Command Line Tools category of the forum

1 Like

Hi @the_stumbler. No, it wouldn't make sense to do that because an Arduino sketch is a folder, not a file:

https://arduino.github.io/arduino-cli/latest/sketch-specification/#sketch-root-folder

Because many Arduino sketches only contain a single .ino file, it's easy to think of that file as the sketch. However, it is the folder that is the sketch. The reason is that sketches may consist of multiple code files and the folder is what groups those files into a single program.

So just store each of your sketches in a separate folder, and pass Arduino CLI the path to that folder.

Thank you @UKHeliBob
Honestly, I looked for it, I'm blind as a bat sometimes.

Well, these are all files belonging to one sketch. I have half a dozen or so separate pairs of files, like filter.cpp and filter.h, along with one main sketch file, say project.ino. This all works just fine.

When I'm making some big changes, and I know the whole project won't compile, it is helpful to just run a single file through the compiler and fix errors only in that file. For example, in the gcc world, gcc -c filter.cpp. I guess there is no equivalent command in Arduino-CLI world?

Which board are you compiling for ?

In this case, it is an RP2350 using Arduino-pico project. This is how I compile the whole sketch.

#!/bin/bash
arduino-cli compile \
  --clean \
  --fqbn rp2040:rp2040:pimoroni_pico_plus_2 \
  --output-dir ./out/

Indeed. The one thing you can do is to make a copy of the sketch, delete all files that are not needed and empty the ino file. Something like

ino

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

somefile.h

void abc();

somefile.cpp

#include <Arduino>
void abc()
{

}

This will compile the ino file as well as somefile.cpp (and throw an error).

Tested in the IDE, I do not use the cli.

Thanks for the creative solution, @sterretje
I think this is a lot of work just to compile one file.
I'm going to give up on this notion.
-Chris

It's actually a lot easier than I described. Create a new sketch (directory and ino file), copy the file that you want compile (and the needed includes) to the new sketch directory and compile the new sketch.

You can even use Arduino CLI to do this:

https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_sketch_new/

1 Like

I'll give this a try, but I don't think it would work well. These .cpp files are just classes, and aren't meant to run stand-alone.

You wanted to compile the .cpp file. It will compile a bit more but not all your files that are in your normal project.