Additional compiler options

Hello,
is there a way, by way of the Arduino IDE (1.8.10), to add additional options to the compiler for the .cpp file ?
In particular I want to add the "-S" option to the compiler invocation line to get the assembler code for my sketch.

I tried adding "-S" to the 'compiler.cpp.extra_flags=' in the 'C:\Program Files (x86)\Arduino\hardware\arduino\avr\platform.txt' file, but to no avail.
Would be great if someone could push me in the right direction. Thanks.

Using compiler.cpp.extra_flags is the correct way to add additional compiler options.

I suspect the reason why it didn't work for you is because you edited the wrong platform.txt. 'C:\Program Files (x86)\Arduino\hardware\arduino\avr' is the location of the Arduino AVR Boards hardware package that comes bundled with the Arduino IDE. But if you update Arduino AVR Boards, it's installed to a different location. Another thing to note is that each hardware package (e.g. Arduino AVR Boards, Arduino SAMD Boards, ESP8266 Core for Arduino) has its own platform.txt file. So if you modify Arduino AVR Boards platform.txt, then compile for one of the Arduino SAMD Boards (e.g., Zero, MKR WiFi 1010), your modification will have no effect.

So the key is to find the active platform.txt for the hardware package of the board you're using. The easiest way to find the active hardware package location is as follows:

  • Select a board from the hardware package you want to find from the Tools > Board menu
  • File > Examples > SPI > BarometricPressureSensor (or any other SPI example sketch)
  • Sketch > Show Sketch Folder
  • Move up folder levels until you reach the one that contains platform.txt

You might be interested to know about this nice feature:

rather than modifying platform.txt directly, you can instead define the compiler.cpp.extra_flags property in a file named platform.local.txt that you place in the same folder as platform.txt. The property definitions in platform.local.txt override those in platform.txt. This is convenient because every time you update to a new version of the hardware package, you need to redo your modifications. So if you save a copy of your platform.local.txt somewhere safe, you can simply copy that file into the hardware package's folder after updating to replace your modifications.

pert:
You might be interested to know about this nice feature:
Arduino IDE 1.5 3rd party Hardware specification · arduino/Arduino Wiki · GitHub
rather than modifying platform.txt directly, you can instead define the compiler.cpp.extra_flags property in a file named platform.local.txt that you place in the same folder as platform.txt. The property definitions in platform.local.txt override those in platform.txt. This is convenient because every time you update to a new version of the hardware package, you need to redo your modifications. So if you save a copy of your platform.local.txt somewhere safe, you can simply copy that file into the hardware package's folder after updating to replace your modifications.

With the caveat that I suspect that some of the Arduino "hangers on" like the Teensy family don't read that file.

I think one additional point is that (provided verbose output is enabled, as pointed out by John) if you look at the command lines displayed in the bottom pane of the Arduino IDE window, you can see the options etc. which are currently in use. If the platform.txt file you're looking at doesn't match, it's unlikely to be the right one :slight_smile:

MarkMLl

Another way to do it is to turn on "Show verbose output during: [X] compilation [ ] upload" in Preferences. That will show you the command line used to compile your sketch (between "Compiling sketch..." and "Compiling libraries..."). For example:

Compiling sketch...
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10810 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/standard -I/Users/john/Documents/Arduino/libraries/IRremote /var/folders/2v/_q7vxn794_d8th6w010cnmv00000gn/T/arduino_build_771667/sketch/sketch_dec25a.ino.cpp -o /var/folders/2v/_q7vxn794_d8th6w010cnmv00000gn/T/arduino_build_771667/sketch/sketch_dec25a.ino.cpp.o
Compiling libraries...

Yes, that is one LONG command line. Copy and paste that command line into a command/terminal window and you can edit it any way you like.

Thanks for your prompt reply.
You were right, I grabbed the wrong "platform.txt" for the Arduino Uno.
I created a "platform.local.txt" in the proper directory, now the compiler option is considered.

MarkMLl:
With the caveat that I suspect that some of the Arduino "hangers on" like the Teensy family don't read that file.

If the Arduino IDE or arduino-cli is used, platform.local.txt will be recognized, even if you're compiling for a 3rd party board. You do need to make sure the platform.local.txt file is in the right folder though. Most Teensy users use Teensyduino, which is a modified version of the Arduino IDE and will recognize platform.local.txt. As for 3rd party IDEs, you would need to check whether they have implemented that feature. Now that we have arduino-cli, it will be even easier for 3rd party IDEs to provide Arduino support that mirrors the behavior of the Arduino IDE. With PlatformIO, platform.local.txt definitely not supported, as they use a completely different system. But PlatformIO provides its own ways of accomplishing this.

pert:
If the Arduino IDE or arduino-cli is used, platform.local.txt will be recognized, even if you're compiling for a 3rd party board. You do need to make sure the platform.local.txt file is in the right folder though. Most Teensy users use Teensyduino, which is a modified version of the Arduino IDE and will recognize platform.local.txt.

I was thinking specifically of the Teensy and my slightly tongue-in-cheek "hangers on" was because of the way it adds stuff to the IDE. So far I've not got platform.local.txt working with that combination (as discussed elsewhere, I've added extra board-recognition definitions to it) but I'll take another look.

MarkMLl

MarkMLl:
I was thinking specifically of the Teensy and my slightly tongue-in-cheek "hangers on" was because of the way it adds stuff to the IDE.

OK, I figured you either meant 3rd party boards or 3rd party IDEs. Teensy are 3rd party boards and Teensyduino might be considered a 3rd party IDE because of the way it modifies the Arduino IDE (though not really that significantly), so Teensy is a bit of both.

MarkMLl:
So far I've not got platform.local.txt working with that combination (as discussed elsewhere, I've added extra board-recognition definitions to it) but I'll take another look.

Is this the elsewhere?:
https://forum.arduino.cc/index.php?topic=653462
If the problem is with getting platform.local.txt working with Eclipse, I'll take your word that it doesn't work. But I just verified that platform.local.txt does work with Teensyduino. The location you need to place platform.local.txt is {Teensyduino installation folder/hardware/teensy/avr. One tricky thing with the Teensy board package is that it doesn't provide those handy "extra_flags" properties for customizing the compilation recipes. You can add those properties into the compilation recipes if you like. Or you could just override the complete compilation recipes in your platform.local.txt. Either one will be a bit more work to maintain, as you'll need to either reimplement the "extra_flags" modifications in platform.txt after every Teensyduino update if you did the former, or make sure to port any changes that were made to the compilation recipes to your platform.local.txt if you did the latter.

pert:
OK, I figured you either meant 3rd party boards or 3rd party IDEs. Teensy are 3rd party boards and Teensyduino might be considered a 3rd party IDE because of the way it modifies the Arduino IDE (though not really that significantly), so Teensy is a bit of both.Is this the elsewhere?:
Determining the board type being used, revisited - Programming Questions - Arduino Forum
If the problem is with getting platform.local.txt working with Eclipse, I'll take your word that it doesn't work. But I just verified that platform.local.txt does work with Teensyduino. The location you need to place platform.txt is {Teensyduino installation folder/hardware/teensy/avr. One tricky thing with the Teensy board package is that it doesn't provide those handy "extra_flags" properties for customizing the compilation recipes. You can add those properties into the compilation recipes if you like. Or you could just override the complete compilation recipes in your platform.local.txt. Either one will be a bit more work to maintain, as you'll need to either reimplement the "extra_flags" modifications in platform.txt after every Teensyduino update if you did the former, or make sure to port any changes that were made to the compilation recipes to your platform.local.txt if you did the latter.

Yes, that's the elsewhere. I raised a bug against Eclipse CDT which makes it their problem, I'll take a look at Teensyduino as per your comments- thanks for that.

MarkMLl

Is there no way to override the compiler options in a file in the same dir as the sketch?
Seems an obvious approach - what am I missing?

You're missing that it can't be done.