Due "-O0" optimisation error: undefined reference to `vtable for HardwareSerial'

I am trying to disable all compiler optimisation for Due. In platform.txt I set all “-Os” to “-O0” (see image for exact locations). Compiling a simple blink sketch gives me error below.

It seems problem is known for Arduino: fix for "undefined reference to `vtable for HardwareSerial'" by EndlessDelirium · Pull Request #336 · sandeepmistry/arduino-nRF5 · GitHub

Any ideas? Many thanks!

Optimisation settings can be set for Due here: https://forum.arduino.cc/index.php?topic=543856.0

N.b. Forum is not letting me post full compiler output.

C:\Users\XXXXXX~1\AppData\Local\Temp\arduino_build_827689/..\arduino_cache_920391\core\core_arduino_sam_arduino_due_x_dbg_eff5f353e5a81c2d8743360c21514f17.a(UARTClass.cpp.o): In function `HardwareSerial::HardwareSerial()':
C:\Users\XXXXXX\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino/HardwareSerial.h:26: undefined reference to `vtable for HardwareSerial'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Due (Programming Port).

2021-04-06 16_05_55-platform.png

Did you try this:

#pragma GCC push_options  // To disable optimization from compiler
#pragma GCC optimize ("O0")

void setup() {
  
}

void loop() {
  
}
#pragma GCC pop_options

Yes I did, but when I do that the compiler output suggests that -Os is still being used as a flag(?) after the file path string:

Compiling sketch...
"C:\\Users\\XXXX\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\arm-none-eabi-gcc\\4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD -mcpu=cortex-m3 -mthumb -DF_CPU=84000000L -DARDUINO=10813 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON "-DUSB_MANUFACTURER=\"Arduino LLC\"" "-DUSB_PRODUCT=\"Arduino Due\"" "-IC:\\Users\\XXXX\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\system/libsam" "-IC:\\Users\\XXXX\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\system/CMSIS/CMSIS/Include/" "-IC:\\Users\\XXXX\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\system/CMSIS/Device/ATMEL/" "-IC:\\Users\\XXXX\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\cores\\arduino" "-IC:\\Users\\XXXX\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\variants\\arduino_due_x" "C:\\Users\\XXXX~1\\AppData\\Local\\Temp\\arduino_build_239963\\sketch\\sketch_apr07a.ino.cpp" -o "C:\\Users\\XXXX~1\\AppData\\Local\\Temp\\arduino_build_239963\\sketch\\sketch_apr07a.ino.cpp.o"
Compiling libraries...

Which gives me the impression nothing is done with the #pragma settings.

With the "empty" sketch provided in reply #1, after compiling, I get:

Sketch uses 10004 bytes with

#pragma GCC optimize ("O0")

Sketch uses 9988 bytes with "O3"

Sketch uses 9980 bytes with "Os"

Hence #pragma does something.

Ah indeed, I get a similar result. Thanks for that!