How to add Sketch compile option -D or /D

Other than editing a sketch or #include file, what is the best and convenient method to set a #define macroname in a specific sketch during compile time ? I'd like to do this when I Verify or Upload or Export Compiled Binary from within the IDE.
Using a reguar C or CPP command line compiler invocation, I'd use the compile option -Dmacroname

I found this: compile - Arduino CLI

but I don't understand where I'd specify the example given if using the IDE,
--build-property build.extra_flags=-DPIN=2

Can someone suggest where I specify the --build-property build.extra_flags argument when using the IDE?
thanks.

Every board family in the Arduino IDE has its own support package with two main config files : `board.txt' (config options for each board) and 'platform.txt' (common build options for whole board family).

For detailed info see

You can't. That flag is for when you are using Arduino CLI to compile your sketch from the command line.

But if you add -D flags to the compilation patterns defined in the platform configuration files mentioned by b077, that change applies to Arduino IDE and Arduino CLI both.

That's good to know, thanks. However, I was hoping there was a dialogue/UI in the IDE to allow me to do it for a particular sketch, but I can understand its not a priority feature given I can just edit the .h file in the IDE.

It looks like you don't understood which file to edit - IDE doesn't contain .h files, you need to change settings in .txt files

Sorry, more specifically, I can edit the .h file of the Sketchbook in the IDE environment and with the IDE editor.

Although it is less common to see this, in addition to the primary .ino file, Arduino sketches can contain .h files. You can add them by clicking the icon on the right side of the editor toolbar, selecting "New Tab" from the menu, and then using the .h file extension when naming the tab. The IDE editor will show any .h files in the sketch as tabs and you can edit their contents. You might use such a file to hold declarations of objects defined in .cpp, .c, or .S files in the sketch, similar to the header files of a library, only not installed as a library.

Editing the sketch code is an effective way of changing macro definitions assuming the affected code is in the sketch. If the affected code is in the source file of a library, core, or toolchain component then the ability to edit the sketch code doesn't help us. That is one of the use case for setting global macros via the -D compiler flag.

and that won't work?

#pragma GCC optimize ("-DPIN=2")

Not that I can tell:

Can you produce a minimal demonstration sketch that shows a macro defined in this manner having an effect on a separate translation unit?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.