CFLAGS on command line

Is there an equivalent of passing CFLAGS from the command line?

Background:
I am using platformio to build for Arduino. My code has some #defines to conditionally compile parts of the code e.g. #define DEBUG to include logging statement etc.

Is there a way I can pass this as a build time option on the command line? Something like make -DDEBUG? I already tried extra_script option in the platformio.ini file with extra_script.py as

from SCons.Script import DefaultEnvironment

env = DefaultEnvironment()
env.Append(CCFLAGS=["-DDEBUG"])

But the option is not getting picked up.

Any suggestions?

Simply use the “build_flags” options in the platformio.ini file and use the -DDEBUG option there.
No need to mess around with the scons build script.

Take a look at this.
Read the build_flags section.

http://docs.platformio.org/en/stable/projectconf.html

That’s one option, but in my case, I want to be able to specify it at build time, because these flags keep changing. DEBUG was one example, where I want to generate two binaries - one with DEBUG enabled and one without.

For ESP8266, if I want to set the WiFi SSID and password (which are defined as macros), but not store them in a file (when I check it in to git).

Okay.
If you already know what flags are going to change beforehand then have a look at the dynamic variables section in the same link.

You can choose to have a [common] section for different environments and simply add the environment specific flags to the subsequent sections.

Then when you run the build process, you can choose to specify the environment or run globally which generates binaries for both the flags.

Please replace

with

Import('env')
env.Append(CCFLAGS=["-DDEBUG"])

As result, you read extra data from private configuratin file. Also, don’t forget that you can pass extra build flags to system environemnt using PLATFORMIO_BUILD_FLAGS.

Thanks Krishna, I’ll keep that in mind for simple configs that I can put in a file

Thanks Ivan, that worked for the main file (main.cpp) but not for the libs, any idea how to pass the same macros when the libs are compiled?

avr-g++ -o .pioenvs/pro8MHzatmega328/src/track.o -c -fno-exceptions -fno-threadsafe-statics -std=gnu++11 -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DDEBUG -DF_CPU=8000000L -DPLATFORMIO=030100 -DARDUINO_ARCH_AVR -DARDUINO_AVR_PRO -DARDUINO=10608 -I/home/me/.platformio/packages/framework-arduinoavr/cores/arduino -I/home/me/.platformio/packages/framework-arduinoavr/variants/eightanaloginputs -Ilib/axTLS -Ilib/debug -I/home/me/.platformio/packages/framework-arduinoavr/libraries/SoftwareSerial/src -Ilib/json -Ilib/Time -Ilib/A67 -Ilib/MQTTPacket -Isrc src/track.cpp

avr-g++ -o .pioenvs/pro8MHzatmega328/lib/A67/A6.o -c -fno-exceptions -fno-threadsafe-statics -std=gnu++11 -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=8000000L -DPLATFORMIO=030100 -DARDUINO_ARCH_AVR -DARDUINO_AVR_PRO -DARDUINO=10608 -I/home/me/.platformio/packages/framework-arduinoavr/cores/arduino -I/home/me/.platformio/packages/framework-arduinoavr/variants/eightanaloginputs -I/home/me/.platformio/packages/framework-arduinoavr/libraries/SoftwareSerial/src -Ilib/debug -Ilib/Time -Ilib/A67 lib/A67/A6.cpp

You can create 2 different build environments in platformio.ini:

[env:release]
board =...

[env:debug]
build_flags = -DDEBUG

Now, pio run -e release and pio run -e debug.

Thanks! That worked perfectly!

If I can bother you a some more, I want to know if I can emulate the following makefile behaviour (I am transitioning to pio, but I am unable to map this usage on my own)

For devices like ESP8266, when using the native SDK, we define the WiFI SSID and password as C macros

#define SSID "myWiFi" //up to 31 ascii characters long
#define SSID_PASSWORD "superSecret" //up to 63 ascii characters long

I don’t want to check the file into git with secrets, when using a makefile I can do something like
make CFLAGS=-DSSID=myWiFi ... during the build

Is there something similar I can do in pio?

See Environment variables — PlatformIO v6.1 documentation

export PLATFORMIO_BUILD_FLAGS="'-DWIFI_PASS=\"My password\"'" 
pio run
1 Like

Thanks again!!

And, sorry I missed it in the docs :sweat_smile: