PlatformIO Community

Compile single source

When working on a large project, even a small typo in a header file can generator massive error messages and the actual error message can get hidden in the sea of aborted compiles.

Is there a way to tell the core or the ide to compile only once source; in orders words one .o?
This would be very helpful in isolating issues that actually affect multiple sources.

It’s possible but it’s rather cumbersome:

Run the compile command manually from a terminal.

In order to find the correct command, run the entire build from the terminal with:

pio run -v

Then search the log for the file that you would like to compile and copy the entire command. Depending on the framework used, it can be quite a long command.

But generally the synatx highlighting (IntelliSense and the like) you warn you early about errors. And if the error only occurs during compilation, go through the log and strictly work top to bottom. That way it’s much easier to spot that an error is just a repeating or subsequent error.

1 Like

I totally agree with Greg that it should be possible to select a single file to compile.

Another solution is to use the -t option
If you run the “pio run” command, then you can identify the target object file you would like to compile.
e.g. if you have the main.cpp within the src folder and you run the “pio run” command, then you shoud see something like the following

Compiling .pio\build\lolin_d32\src\main.cpp.o

So if you just want to build the main.cpp file, you can invoke pio as follows:
pio run -t .pio\build\lolin_d32\src\main.cpp.o

In that way, you can refactor your code in a more professional and also faster way :slight_smile:

Thanks for the tip. I just tried this and wanted to elaborate a bit for anyone else looking for this capability. I use pio run -v and got the command I wanted. It was very long! And when I pasted it in the terminal window the result was

bash: xtensa-esp32-elf-g++: command not found

The command started with

xtensa-esp32-elf-g++ -o .pio/build/esp32doit-devkit-v1/src/wifi.o

And I found that running

pio run -t .pio/build/esp32doit-devkit-v1/src/wifi.o

compiled the single file. In general it looks like that pattern could be applied to any source file.


This only works when the compiler used by PlatformIO (~/.platformio/packages/toolchain-xtensa32/bin) is also added to your PATH, you have to do yourself if you start your own bash session.

No mention of that in the original post so I wasn’t sure if it reflected a change in PlatformIO or was always like that. I was using the terminal internal to VS Code and was expecting everything to be fully configured but clearly it was not. That left me wondering what other environment variables were not set. It seems clear that the pio run command sets the complete environment and provides a convenient way to compile a single file.


Absolutely, pio run -t <target file> is a really neat way of doing that, way better than copy-pasting really long compiler invocations in the terminal. Nice find actually!