ESP32 unused variable compile error

Going back to some code for the ESP32 that I had previously used, it will now not compile

Simplifying the code down to

void setup()
{
  byte foo;
}

void loop()
{
}

produces this error

Arduino: 1.8.13 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, Core 1, Core 1, None, Disabled"


C:\Users\Bob2\AppData\Local\Temp\arduino_modified_sketch_992452\sketch_oct06b.ino: In function 'void setup()':

sketch_oct06b:3:8: error: unused variable 'foo' [-Werror=unused-variable]

   byte foo;

        ^~~

cc1plus.exe: some warnings being treated as errors

exit status 1

unused variable 'foo' [-Werror=unused-variable]

This is using IDE 1.8.13 with the ESP 32 Dev board as its target and the ESP board files version 2.0.5 which is the latest offered by the IDE and which has been installed for some time. Reverting to version 2.0.4 of the ESP32 boards file produces the same error but the code compiles with version 2.0.3 and not even a warning is produced. All compiler warnings are turned on

Unsurprisingly the code compiles, albeit with a warning, when the target is an AVR such as the Nano

I can find no reference to this problem in the forum or online, but can anyone else confirm that the problem exists for them, or not, of course ?

can you set the preferences so that warnings aren't treated as errors?

there's a reason treating warnings as errors, which i believe is very good practice, is optional. there's a need to be able to compile older working code that has acceptable flaws.

Does using the variable produce the error?

byte foo=0;
foo++;
Serial.println(foo);

did you try with 1.8.19 ?

it's a consequence of using the -Werror flag during compilation...

at the end of the day, if the variable is unused, get rid of it :slight_smile:

if it's for later, you could possibly add __attribute__((unused)) to tell the compiler you know what you are doing

This fails

void setup()
{
  Serial.begin(115200);
  byte foo = 0;
}

void loop()
{
}

This compiles

void setup()
{
  Serial.begin(115200);
  byte foo = 0;
  foo++;
}

void loop()
{
}

as does this

void setup()
{
  Serial.begin(115200);
  byte __attribute__((unused)) foo = 0;
}

void loop()
{
}

The results are the same in 1.8.19 as in 1.8.13 and 2.0.0 for that matter

1 Like

I've noticed it during recent ESP32 compilation attempts.

I don’t have the issue with 1.8.19 and a Heltec LoRa ESP32

Thanks for the feedback

It is obviously easy to avoid the problem but a shame that the problem was introduced

That's interesting, because I do if I set the target board to Heltec LoRa ESP32 or Heltec LoRa ESP32(V2)

it was not the V2 but I tried again with that one and this code

void setup()
{
  Serial.begin(115200);
  byte foo = 0;
}

void loop()
{
}

and it compiled just fine (macOS Monterey 12.6)

I've this link for the ESP32 boards

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
so this is "version": "2.0.5",

EDIT: the link is 2.0.5 but I did not run the update so I've 2.0.3 actually :innocent: :roll_eyes: :woozy_face:. let me try to update

EDIT: after upgrading ➜ confirmed the same error popped up

platform.txt for ESP32 contains

# Arduino Compile Warning Levels
compiler.warning_flags=-w
compiler.warning_flags.none=-w
compiler.warning_flags.default=
compiler.warning_flags.more=-Wall -Werror=all
compiler.warning_flags.all=-Wall -Werror=all -Wextra

so setting the preferences to all warning selects -Werror=all

can you post the build line to see what options are being set?

It’s the

the latter triggers this

i agree. are the default options different in the 2 versions of the IDE

Added compiler.warning_flags to platform.txt; fixing #6118 (#6596) · espressif/arduino-esp32@ca77502 · GitHub

hum too fast, was not that one :slight_smile:

let me dig

I saw this ➜ https://github.com/espressif/arduino-esp32/pull/7060

seems complicated :slight_smile:

but light at the end of the tunnel (and not a train) maybe

Detailed instructions for Windows:

Go to C:\Users\myName\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5 (or whatever version is current)

Make a copy of platform.txt and rename it to platform.local.txt.
Inside platform.local.txt, change the Compiler Warning levels from:

compiler.warning_flags=-w
compiler.warning_flags.none=-w
compiler.warning_flags.default=
compiler.warning_flags.more=-Wall -Werror=all
compiler.warning_flags.all=-Wall -Werror=all -Wextra

To:

compiler.warning_flags=-w
compiler.warning_flags.none=-w
compiler.warning_flags.default=
compiler.warning_flags.more=-Wall
compiler.warning_flags.all=-Wall -Wextra

This works when you set the Arduino Compiler warnings to 'More’ or 'All'. Tested on both 1.8.16 and 2.03 Arduino IDE.

Vern

3 Likes

What I don't understand is that this objectionable behavior happens in IDE v1.8.15 but not v1.8.19. However the 'platform.txt' file is specific to the ESP32 board package and I have the same package v2.0.6 installed in both of the above IDE versions.

arduino 1.8.19, esp32 boards 2.0.7, still happens. Grr. Wish this was an easier preference to set.

variable 'got' set but not used [-Werror=unused-but-set-variable]

I tried commenting the declaration. It turns out the variable was indeed used later on and I got a true error. This is using ide 2.1.0 on code that worked on a 8266 and trying on a C3.

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