# How to add Sketch compile option -D or /D

**URL:** <https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975>\
**Category:** Arduino Command Line Tools\
**Created:** [February 18, 2023, 9:55pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975 "2023-02-18T21:55:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![blouchip](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blouchip/32/549509_2.png) [@blouchip](https://forum.arduino.cc/u/blouchip)\
**Post date:** [February 18, 2023, 9:55pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/1 "2023-02-18T21:55:14Z")

</div>

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](https://arduino.github.io/arduino-cli/0.30/commands/arduino-cli_compile/)

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.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [February 18, 2023, 10:06pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/2 "2023-02-18T22:06:26Z")

</div>

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

> **[Platform specification - Arduino CLI](https://arduino.github.io/arduino-cli/0.19/platform-specification/)**
>
> Arduino Command Line Interface

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [February 18, 2023, 10:17pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/3 "2023-02-18T22:17:10Z")

</div>

> [@blouchip](#):
>
> Can someone suggest where I specify the --build-property build.extra\_flags argument when using the IDE?

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.

---

<div class="post-metadata">

**Author:** ![blouchip](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blouchip/32/549509_2.png) [@blouchip](https://forum.arduino.cc/u/blouchip)\
**Post date:** [February 18, 2023, 11:28pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/4 "2023-02-18T23:28:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [February 19, 2023, 12:33am UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/5 "2023-02-19T00:33:00Z")

</div>

> [@blouchip](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![blouchip](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blouchip/32/549509_2.png) [@blouchip](https://forum.arduino.cc/u/blouchip)\
**Post date:** [February 19, 2023, 12:41am UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/6 "2023-02-19T00:41:58Z")

</div>

> [@blouchip](#):
>
> given I can just edit the .h file in the IDE.

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

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [February 19, 2023, 12:17pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/7 "2023-02-19T12:17:44Z")

</div>

> [@b707](#):
>
> IDE doesn't contain .h files

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.

---

<div class="post-metadata">

**Author:** ![ua6em](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ua6em/32/930389_2.png) [@ua6em](https://forum.arduino.cc/u/ua6em)\
**Post date:** [February 19, 2023, 1:25pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/8 "2023-02-19T13:25:47Z")

</div>

and that won't work?

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

```

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [February 19, 2023, 2:26pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/9 "2023-02-19T14:26:01Z")

</div>

Not that I can tell:

> **[Function Specific Option Pragmas (Using the GNU Compiler Collection (GCC))](https://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html)**
>
> Function Specific Option Pragmas (Using the GNU Compiler Collection (GCC))

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 18, 2023, 2:26pm UTC](https://forum.arduino.cc/t/how-to-add-sketch-compile-option-d-or-d/1091975/10 "2023-08-18T14:26:42Z")

</div>

Closed on August 18, 2023, 2:26pm UTC

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