Build two projects with different defines

Hi,

I have got a project I am working on. The project is suitable for two different implementation using IFDEF in the code.
At the moment I am currently commenting out the define in the code.
I was wondering if there is a way to compile both settings each time and create two binaries, one for each setting.

Maybe you could add compilation macros with the build_flags parameter and divide both implementations by creating different environments in the platformio.ini file like this:

[env:setup_1]
build_flags = -D__FLAG1__

[env:setup_2]
build_flags = -D__FLAG2__

Then, in your code put something like this:

#ifdef __FLAG1__
//something 2
#endif

#ifdef __FLAG2__
//something 2
#endif
1 Like

Possibly take one step back… it sounds like the flag is already there, but the OP is commenting out commenting the define. Instead, remove the define from the source file completely, and set it via the build_flags as you mentioned using two build environments. Then either do something only when the flag is set, or explcity check if it’s a 1 or a 0, etc. Doing so will create two binaries, one with the ifdef code, one without.

For instance, if it’s flag that enables or disables serial output…

#ifdef SERIAL_OUTPUT
Serial.println("Serious debug info!");
#endif

and in your platformio.ini, either declare SERIAL_OUTPUT or don’t… (I added VERSION just so the build_flags wasn’t empty the second time… you can omit build_flags entirely if you don’t have any other DEFINES to create).

[env:serial_env]
build_flags = -DSERIAL_OUTPUT -DVERSION=1

[env:noserial_env]
build_flags = -DVERSION=1

Thanks a lot to you both. This was simple and easy…

1 Like