0

I am programming esp32 microcontrollers using esp-idf. I have noticed this strange compiler behaviour when building my code. When I declare some variables and not use them, sometimes I get an error instead of warning. For example when I declare a variable:

static const uint16_t WRITE_CHARACTERISTIC       = 0x0011;

But I do not use it anywhere in my code, I get an error:

../components/BLE/BLE_custom.c:131:23: error: 'WRITE_CHARACTERISTIC' defined but not used [-Werror=unused-const-variable=]
 static const uint16_t WRITE_CHARACTERISTIC       = 0x0011;
                       ^~~~~~~~~~~~~~~~~~~~
cc1.exe: some warnings being treated as errors
ninja: build stopped: subcommand failed.
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command cmake --build ." terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

If I comment this line out, the code works fine. Could someone help me understand why this happens and how to fix this.

Full log:

Executing task: cmake --build . <

[1/9] Performing build step for 'bootloader'
ninja: no work to do.
[3/7] Building C object esp-idf/BLE/CMakeFiles/__idf_BLE.dir/BLE_custom.c.obj
FAILED: esp-idf/BLE/CMakeFiles/__idf_BLE.dir/BLE_custom.c.obj 
C:\Users\my_user\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" -Iconfig -I../components/BLE -IC:/Users/petrikas.lu/esp/esp-idf/components/newlib/platform_include -IC:/Users/petrikas.lu/esp/esp-idf/components/freertos/include -IC:/Users/petrikas.lu/esp/esp-idf/components/freertos/port/xtensa/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_hw_support/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_hw_support/port/esp32/. -IC:/Users/petrikas.lu/esp/esp-idf/components/heap/include -IC:/Users/petrikas.lu/esp/esp-idf/components/log/include -IC:/Users/petrikas.lu/esp/esp-idf/components/lwip/include/apps -IC:/Users/petrikas.lu/esp/esp-idf/components/lwip/include/apps/sntp -IC:/Users/petrikas.lu/esp/esp-idf/components/lwip/lwip/src/include -IC:/Users/petrikas.lu/esp/esp-idf/components/lwip/port/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/lwip/port/esp32/include/arch -IC:/Users/petrikas.lu/esp/esp-idf/components/soc/include -IC:/Users/petrikas.lu/esp/esp-idf/components/soc/esp32/. -IC:/Users/petrikas.lu/esp/esp-idf/components/soc/esp32/include 
-IC:/Users/petrikas.lu/esp/esp-idf/components/hal/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/hal/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_rom/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_rom/esp32 -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_common/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_system/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/driver/include -IC:/Users/petrikas.lu/esp/esp-idf/components/driver/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_ringbuf/include -IC:/Users/petrikas.lu/esp/esp-idf/components/efuse/include -IC:/Users/petrikas.lu/esp/esp-idf/components/efuse/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/xtensa/include -IC:/Users/petrikas.lu/esp/esp-idf/components/xtensa/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/espcoredump/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_timer/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_ipc/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_pm/include -IC:/Users/petrikas.lu/esp/esp-idf/components/vfs/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_wifi/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_wifi/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_event/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_netif/include -IC:/Users/petrikas.lu/esp/esp-idf/components/esp_eth/include -IC:/Users/petrikas.lu/esp/esp-idf/components/tcpip_adapter/include -IC:/Users/petrikas.lu/esp/esp-idf/components/app_trace/include -IC:/Users/petrikas.lu/esp/esp-idf/components/bt/common/osi/include -IC:/Users/petrikas.lu/esp/esp-idf/components/bt/include/esp32/include -IC:/Users/petrikas.lu/esp/esp-idf/components/bt/host/bluedroid/api/include/api -IC:/Users/petrikas.lu/esp/esp-idf/components/nvs_flash/include -IC:/Users/petrikas.lu/esp/esp-idf/components/spi_flash/include -IC:/Users/petrikas.lu/esp/esp-idf/components/mbedtls/port/include -IC:/Users/petrikas.lu/esp/esp-idf/components/mbedtls/mbedtls/include -IC:/Users/petrikas.lu/esp/esp-idf/components/mbedtls/esp_crt_bundle/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -fstrict-volatile-bitfields -Wno-error=unused-but-set-variable -std=gnu99 -Wno-old-style-declaration -D_GNU_SOURCE -DIDF_VER=\"v4.3-dirty\" -DESP_PLATFORM -MD -MT esp-idf/BLE/CMakeFiles/__idf_BLE.dir/BLE_custom.c.obj -MF esp-idf\BLE\CMakeFiles\__idf_BLE.dir\BLE_custom.c.obj.d -o esp-idf/BLE/CMakeFiles/__idf_BLE.dir/BLE_custom.c.obj   -c ../components/BLE/BLE_custom.c
../components/BLE/BLE_custom.c:131:23: error: 'WRITE_CHARACTERISTIC' defined but not used [-Werror=unused-const-variable=]
 static const uint16_t WRITE_CHARACTERISTIC       = 0x0011;
                       ^~~~~~~~~~~~~~~~~~~~
cc1.exe: some warnings being treated as errors
ninja: build stopped: subcommand failed.
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command cmake --build ." terminated with exit code: 1.
TheBestPlayer
  • 324
  • 2
  • 13
  • That is usually only a *warning*. You get an error because you have turned that warning (or more likely *all* warnings) into an error. You need to look at your build configuration to see how it's set up. And if you only want a warning for this case, you can disable the error for this specific warning (`-Wno-error=unused-const-variable`). – Some programmer dude Sep 01 '21 at 06:04
  • Can you explain what do you mean "turned". I didint change anything and as I have mentioned before, this is not always the case. It sometimes allows me to initialise variables wihtout using them and not give me any errors so I am a little confused why it sometimes gives me an error – TheBestPlayer Sep 01 '21 at 06:06
  • _I didint change anything_ Then, somebody else did it. It's even noted in your error report: `cc1.exe: some warnings being treated as errors` – Scheff's Cat Sep 01 '21 at 06:08
  • How do you build? What is your project configuration? What extra flags are passed to the compiler? Can you show a snippet from the verbose build log (where it shows what commands are being used, including all flags and options and arguments), especially for `../components/BLE/BLE_custom.c`? – Some programmer dude Sep 01 '21 at 06:09
  • The esp-idf using cmake to build project. I am reading more about cmake and compiler to understand what really happens and how to fix it. I have put a full logs in my initial post – TheBestPlayer Sep 01 '21 at 06:50
  • When building there's the option `-Werror=all`, followed by a few `-Wno-error=...` options, but not one for your error. So that's why you get an error for this "warning". You need to look in the `CMakeList.txt` file to see where compiler options are being set. Look for `-Wno-error=` and add the option to disable the error you get. – Some programmer dude Sep 01 '21 at 07:14
  • 1
    Can you post an example when you **don't** get a an error with an unused variable, please? – the busybee Sep 01 '21 at 07:37
  • I have declared a variable and not using it anywhere ```int test = 5;``` Not even a warning comes out of this! Looks like the errors and warnings appear only for certain unused types such as const char as previously shown – TheBestPlayer Sep 01 '21 at 08:07
  • The warning flag for this specific warning (turned into an error) is `unused-const-variable`. Notice the **`const`** part of the option. – Some programmer dude Sep 01 '21 at 08:09
  • Thanks for clarifying. Adding the following command under my component CMakeLists.txt fix the issue : ```target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable)```. I am still trying to find a way how to set the compile option globally for a whole program instead of just a BLE_custom component. – TheBestPlayer Sep 01 '21 at 09:42
  • FYI: [SO: How do I add a linker or compile flag in a CMake file?](https://stackoverflow.com/q/11783932/7478597) – Scheff's Cat Sep 02 '21 at 08:46

0 Answers0