How to add compile options to Arduino IDE 1.8.13

I have found a lot telling me to add the options to global.txt platform.txt, platform.local.txt and so on.

I want to add an option that will be added to the compile options for all boards.

Here is the Arduino CLI command:

arduino-cli compile --fqbn arduino:avr:mega --build-property build.extra_flags=-DOSH_NOXMLNS Unified_Sensor_ArduinoMEGA_ATWINC1500B_Test29.ino

If you notice in the line, the option I want to add to all my boards is "--build-property build.extra_flags=-DOSH_NOXMLNS"

It is unclear to me where the global.txt file is (what folder) and the syntax to put into the file.

I have the Arduino IDE installed on a Windows 10 OS in the default install location.

Please help.

To affect all boards, you can add this property to the global platform.txt:
https://arduino.github.io/arduino-cli/latest/platform-specification/#global-platformtxt
So if your Arduino IDE installation is at "C:\Program Files (x86)\Arduino" then the location of the global platform.txt is "C:\Program Files (x86)\Arduino\hardware\platform.txt"

A couple things to note though:

build.extra_flags property is intended for use by the platform developer. What this means is that you may end up overriding important flags with your custom definition. Here's an example:

leonardo.build.extra_flags={build.usb_flags}

and the definition of build.usb_flags here:

build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}'

So use build.extra_flags cautiously.

From reading the comments in the official platform level platform.txt files:

# This can be overridden in boards.txt
build.extra_flags=

# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.S.extra_flags=
compiler.cpp.extra_flags=
compiler.ar.extra_flags=
compiler.objcopy.eep.extra_flags=
compiler.elf2hex.extra_flags=

it seems that the compiler.x.extra_flags properties are intended for use by the user (because platform developers have no need for platform.local.txt). So you are better off to use those properties. Unfortunately, some platform developers have unnecessarily used these properties as well. For example:

but it's at least less common than build.extra_flags.

There is some interesting discussion on how this situation might be improved here:

Thank you fpr your reply, I think I finally got it. I added a file called "platform.txt" to the "C:\Program Files (x86)\Arduino\hardware" folder where Arduino IDE is installed. The file contains the following line:

build.extra_flags=-DOSH_NOXMLNS

I shutdown all IDEs and restarted Arduino IDE and did a compile and push to the MEGA. I checked the receiving server and it seems to be working.

I think I get that the build options will cause problems if something else needs to use them. If other options are needed will they still be appended to these or will this be the only option used since it is the only line in the file?

packetkiller:
I think I get that the build options will cause problems if something else needs to use them. If other options are needed will they still be appended to these or will this be the only option used since it is the only line in the file?

I'm not sure I understand your question. What I'll do it provide a better explanation of what the problem is.

Your property definition in the global platform.txt overrides previous definitions of that property.

I'll use the Leonardo board definition I mentioned before as an example, but other boards use it for even more important purposes. The Leonardo board definition contains this line:

leonardo.build.extra_flags={build.usb_flags}

It's a little confusing because this property definition references another property, which references other properties, but all that is irrelevant to this discussion. You only need to understand that in the end this causes the build.extra_flags property to have this value when compiling for the Leonardo:

-DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\""

Those flags are added to the compilation commands.

Now, when you add a definition of build.extra_flags to the global platform.txt, your definition overrides the definition that property was expected to have. So instead of the compilation commands containing this while compiling for the Leonardo:

-DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\""

it contains this:

-DOSH_NOXMLNS

I used the Leonardo as an example, but many other boards use build.extra_flags for various purposes.

build.extra_flags could be used carefully on a case-by-case basis to inject custom flags into the compilation commands after checking to see if and how the specific board you're compiling for is already using that property. For example, if you're compiling for the Mega, you would find that it doesn't use build.extra_flags and feel free to use it as you like without the chance of overriding. In the case of the Leonardo, you could find the standard definition of the property and include that in your custom definition:

build.extra_flags={build.usb_flags} -DOSH_NOXMLNS

But it's not possible to do this with a global definition of the property.

This is why I recommended you to use the "extra flags" properties that are specifically intended for this use.

I think I see your point. Thank you for continuing to help me understand. So to restate it in my simple terms, the higher in the over ride (global being the top), the more likely some board options may be replaced by the manual override compile option. So when ever possible, try to go down to the board type you are using (platform.local.txt) for a sketch and over ride it there instead.

Also keeping in mind that even at a lower level, the option you inject using your platform.local.txt should ensure it includes all the other options that may be there by default, so you do not accidently override options you may still need.

Did I state that close enough?

And it does not sound like there is an option at the sketch level at this time? Unless you use Arduino CLI each sketch compile?

Thanks again for helping me understand this.

packetkiller:
Also keeping in mind that even at a lower level, the option you inject using your platform.local.txt should ensure it includes all the other options that may be there by default, so you do not accidently override options you may still need.

Did I state that close enough?

Yes, but since the build.extra_flags property is defined differently for each board, it's not possible (or at least not simple) to do this in platform.local.txt because the definition there affects all boards of the platform equally. You would be better to use boards.local.txt. So to add your flag to build.extra_flags for Mega and Leonardo, you would add this boards.local.txt file to the Arduino AVR Boards platform folder:

mega.build.extra_flags=-DOSH_NOXMLNS
leonardo.build.extra_flags={build.usb_flags} -DOSH_NOXMLNS

Of course, you could do the same for the rest of the boards of the platform, this is just a basic example.

packetkiller:
And it does not sound like there is an option at the sketch level at this time? Unless you use Arduino CLI each sketch compile?

You can define properties using the --pref option of the Arduino IDE's CLI just as you would with the --build-property option of Arduino CLI, but there's no way to do it via the Arduino IDE's GUI.

packetkiller:
Thanks again for helping me understand this.

I'm glad if I can be of assistance. It's a pretty complex topic, but I find it to be pretty interesting. Fortunately, it's the sort of thing beginners have no need of knowing, while allowing advanced users to do advanced things.

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