Define build flag "on the fly"

Hi,
actually to pass some macro at compile time I’ve edited the build_flags variable in my platformio.ini like this:

build_flags = -D MACRO1=value1 -D MACRO2=value2 ...[other macros]

The problem is that I have to edit the platformio.ini file every time I want to change one of these macros.
It is possible to define them “on the fly” without changing the configuration file?
Thank you

Instead of doing things like

void my_function() { 
  int something = MACRO1 * 100 + 2;
  Serial.println(something);
}

and injecting the macro value via a build_flags = -D MACRO1=123, you can also just create a dedicated header file which the code that uses this macro will include. Thus you can change the macros in the file and no complete recompile takes place.

E.g.,

#include <my_settings.h>
void my_function() { 
  int something = MACRO1 * 100 + 2;
  Serial.println(something);
}

my_settings.h

#ifndef MY_SETTINGS_H
#define MY_SETTINGS_H

#define MACRO1 123

#endif MY_SETTINGS_H

If your configuration macro is more target-board bound, you can also create multiple different environments and select the one you’re compiling for. See docs Redirecting...

1 Like

Thank you, but this is not what I’m trying to do…
I need to change the macro at compile time without modifying other files

I can’t really follow here. How are you going to change the macro value while “not modifying other files”? You have to modify something. Either your platformio.ini or the .h the macro is defined in.

Well a third way to do it would be to write a python script to inject the defines. See docs. But either the first or second way are nicer than this complicated third way…

1 Like

And don’t forget, an edit to platformio.ini triggers a full rebuild, so either having the compile-time change done by choosing a different [env:] to build or header file would be quicker, as it would require only minimal re-compile. :wink:

To me is normal to define some macro on the fly. Without platformio when I want to compile some code I make a call to gcc and pass the macro directly with the gcc command. This is very useful in my case because this macro value is, more or less, different for every compilation. So I want to be sure that every time I compile the code with a different value.

Using a file would be one solution, but the risk is to compile the code with old values.
A possible solution is to comment the line where the macro is defined after each compilation, so when I try to recompile it give me an error bacause the macro is not defined. This force me (or someone else) to uncomment the line and change the macro value.
I’m only trying to find a way to do this at compile time, so instead of changing a file and clicking the “build” button I must input (in some way) the macro value for the code to be compiled.
I hope I have explained the problem better, probably there is a simple way to do this

Best option is probably a header file or macro you comment/uncomment. You could probably make a extra_script via the Advanced Scripting that did it automatically… i.e. commented it out when the build completes… but is it worth the effort? Alternately, some sort of interactive prompt that uses the Dynamic Build Flags, but again… is it worth the effort?