-Werror only working on manual preprocessor #warning

I’m trying to configure a project for Arduino Nano ESP32. In the platformio.ini file I have enabled warnings as errors but it only seems to work if I have a manually defined preprocessor warning in the code. I still get normal warnings but the do not trigger as errors. I have created a very simple test project to try and figure the issue out.

Full platformio.ini file:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:arduino_nano_esp32]
platform = espressif32
board = arduino_nano_esp32
framework = arduino
monitor_speed = 115200
build_flags =
    -Wall
    -Wextra
    -Werror

Full main.cpp file:

#include <Arduino.h>

void setup() {
  int x;

  #warning no

  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

If I compile the code this is the resulting log:

Processing arduino_nano_esp32 (platform: espressif32; board: arduino_nano_esp32; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/arduino_nano_esp32.html
PLATFORM: Espressif 32 (6.12.0) > Arduino Nano ESP32
HARDWARE: ESP32S3 240MHz, 320KB RAM, 16MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, dfu, esp-bridge, esp-builtin, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 @ 3.20017.241212+sha.dcc1105b 
 - tool-dfuutil-arduino @ 1.11.0
 - tool-esptoolpy @ 2.40900.250804 (4.9.0)
 - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
 - toolchain-xtensa-esp32s3 @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 33 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\arduino_nano_esp32\src\main.cpp.o
src/main.cpp:6:4: error: #warning no [-Werror=cpp]
   #warning no
    ^~~~~~~
src/main.cpp: In function 'void setup()':
src/main.cpp:4:7: warning: unused variable 'x' [-Wunused-variable]
   int x;
       ^
cc1plus.exe: all warnings being treated as errors
*** [.pio\build\arduino_nano_esp32\src\main.cpp.o] Error 1
========================================================================================================= [FAILED] Took 2.55 seconds =========================================================================================================

In short I get one error for the manual “no” warning.

If I comment out the manual warning this is the result:

Processing arduino_nano_esp32 (platform: espressif32; board: arduino_nano_esp32; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/arduino_nano_esp32.html
PLATFORM: Espressif 32 (6.12.0) > Arduino Nano ESP32
HARDWARE: ESP32S3 240MHz, 320KB RAM, 16MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, dfu, esp-bridge, esp-builtin, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 @ 3.20017.241212+sha.dcc1105b
 - tool-dfuutil-arduino @ 1.11.0
 - tool-esptoolpy @ 2.40900.250804 (4.9.0)
 - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
 - toolchain-xtensa-esp32s3 @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 33 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\arduino_nano_esp32\src\main.cpp.o
src/main.cpp: In function 'void setup()':
src/main.cpp:4:7: warning: unused variable 'x' [-Wunused-variable]
   int x;
       ^
Retrieving maximum program size .pio\build\arduino_nano_esp32\firmware.elf
Checking size .pio\build\arduino_nano_esp32\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   9.4% (used 30656 bytes from 327680 bytes)
Flash: [=         ]   9.3% (used 291189 bytes from 3145728 bytes)
========================================================================================================= [SUCCESS] Took 2.65 seconds =========================================================================================================

I get a warning for the unused variable but it doesn’t get treated as an error as it should.

Any idea what I’m doing wrong?

You set -Werror but the default CCFLAGS that are set by the Arduino-ESP32 builder script sneakily add an exception

So you have to unset those with an additional

build_unflags =
  -Wno-error=unused-function
  -Wno-error=unused-variable
  -Wno-error=deprecated-declarations
  -Wno-unused-parameter
  -Wno-sign-compare
  -Wno-error=unused-but-set-variable
1 Like

Thank you! Good to know that there is a bunch of stuff like this in the builder script..