Stuck on how to use external .ini files in platformio.ini

Hi, I’m new to platformio.
I am trying to figure out how to add some extra flags to my platformio.ini file by importing them from an external file, similar to how #include works in c.
I’ve spent a while searching the docs and forum, but just getting myself confused about sections and envs etc

so far I have this platformio.ini file:.

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

build_flags =
  -D MY_TEST_LOCAL_DEF=22

[MySection]
extra_configs =
  D:/Projects/PlatformIO/Board_Configs/test_extras.ini

and this external test_extras.ini file:

[MySection]
build_flags =
  -D MY_TEST_EXTERN_DEF=33

and this is my Arduino code:

#include <Arduino.h>

int x = MY_TEST_LOCAL_DEF;
int y = MY_TEST_EXTERN_DEF;

void setup() {}
void loop() {}

When I compile it says MY_TEST_EXTERN_DEF is undefined, so it’s not seeing the included file. Obviously I have a lot to learn, but was hoping someone would point me in the right direction.

Referring to the documentation, extra_configs must be placed into the [platformio] section. So the issue is placing it into the [MySection] section.

See extra_configs — PlatformIO latest documentation

Thanks sivar. I read through the docs again and came up with this:

platformio.ini:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

build_flags =
  -D MY_TEST_LOCAL_DEF=22

[platformio]
extra_configs =
  D:/Projects/PlatformIO/Board_Configs/test_extras.ini

test_extras.ini:

[env:esp32dev]
build_flags =
  -D MY_TEST_EXTERN_DEF=33

Now, the compiler finds the external def, but not the one in platformio.ini.
The docs indeed say that if the group name matches they will be overwritten, although I assumed that just meant any flags of the same name, not that it would erase all of the flags and only use the external ones.
But it seems I have to use the same group name in the external file or my code doesn’t see the entries anyway.
Is there a way to have both sets of flags visible to my code?

I don’t know if interpolation and “extends” works with extra-config files. Give it a try…

Please see Interpolation of Values — PlatformIO latest documentation

and extends — PlatformIO latest documentation

Thanks Sivar for the hints. After quite some trial and error, Interpotation indeed turned out to be the solution:

platformio.ini

[platformio]
extra_configs =
  D:/Projects/PlatformIO/Board_Configs/test_extras.ini

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

build_flags =
  -D MY_TEST_LOCAL_DEF=22
  ${my_extras.build_flags}

test_extras.ini

[my_extras]
build_flags =
  -D MY_TEST_EXTERN_DEF=33

Arduino code:

#include <Arduino.h>

int x = MY_TEST_LOCAL_DEF;
int y = MY_TEST_EXTERN_DEF;

void setup() 
{
  Serial.begin(115200);
  Serial.printf("Vals = %d, %d\r\n", x, y);
}
void loop() {}

Result:

Vals = 22, 33

I don’t know if this is the only / best solution, but it’s doing what I want.
This allows me to compile for a variety of custom boards with different pinouts by only changing one ot two lines in the platformio.ini file.

2 Likes