Why this obviously not correct warning

I recieve this warning: “warning: variable ‘coder’ set but not used”, while, as we look in the [partial] code, the variable ‘coder’ is perfectly used.
#ifndef ROTARY_ENCODER_TASK_H
#define rotaryEncoderTask_h

// Define the semaphore here and give it a value in main.cpp so it
// can be used in this task.
//
extern SemaphoreHandle_t sMutex;

struct Coder {
uint8_t index; // index of this radio station to display
bool select; // radio station with this index to play
};

#include <tasksConfig.h> // includes rotaryEncoderTask.h
#include <stdint.h> // for uint8_t

void rotaryEncoderTask(void *param)
{
// warning: variable ‘coder’ set but not used [-Wunused-but-set-variable]
Coder coder;

// For-Ever-Loop
// =============
//
for(;;)
{      
    if (oldState != newState)
    {
        if (newState == 3)
        {                 
            if (oldCoderIndex != newCoderIndex)
            {
                if (sMutex != NULL)
                {
                    if (xSemaphoreTake(sMutex, (TickType_t) 10) == pdTRUE)
                    {
                        **coder.index**  = newCoderIndex;  
                        **coder.select** = false;

In your code, the variable coder is assigned a value, but – at least in the excerpt shown – never read or otherwise used. So the assignment has no effect and the variable could be removed altogether. That’s what the compiler is saying and – trust me – it’s usually right.

Since you are using a mutex to protect access to the variable, I’m assuming there is other code that will access the variable too. However, coder is currently declared as a local variable within the rotaryEncoderTask function. Is coder supposed to be a global variable? If so, you need to move the declaration outside the function.

Thank you. My mistake was that I have to make the struct [as defined in a header file] global, this way:
struct Coder {
uint8_t index; // index of this radio station to display
bool select; // radio station with this index to play
};
extern Coder coder;

Still not compiling well:
.pio/build/esp32dev/src/main.cpp.o:(.literal._Z4loopv+0x0): undefined reference to `coder’

Here you write

but in the entirety of https://github.com/Aleph-Design/Rotary-Encoder-FreeRTOS-00/blob/master/src/rotaryEncoderTask.cpp there is no

Coder coder;

line which would define the variable. Thus it’s a declaration of existance (the extern Coder coder does exactly that – tell it that this thing exists ‘somewhere’) but it was never defined (that is, the global variable must be created in one .cpp file, optionally with an initial value) and gives you that error.

Thanks for the reply.
But, IMHO that’s exactly what I tried to do.

In file tasksConfig.h on line 20 I’ve defined globally:
extern Coder coder;

Then in file rotaryEncoderTask.cpp I tried to initialize coder
on lines 35 and 36:
coder.index = 0;
coder.select = false;
When I mouseover the “index” part of coder here I get:
“variable ‘coder’ set but not used [-Wunused-but-set-variable]”
And that’s also what the compiler says.

Then on lines 95 and 96 the mouseover shows a perfect normal respons saying:
uint_t Coder::index; as expected.

So, I’m pretty much confused.

In order to make the variable coder global, put the variable definition outside any function like so:

Coder coder;

void rotaryEncoderTask(void *param)
{
    ...

If you want to access the same variable from another file, you can additionally add a declaration in a header file, e.g.:

extern Coder coder;

C and C++ distinguish between declaration and definition:

  • Declaration: Provides the information that a variable or function exists and what name, type and parameters they have. But it does not actually allocate the variable or produce any code for the function.

  • Definition: Allocates a variable and initializes it (or produces the code for a function). In addition, it also declares the variable or function.

The keyword extern basically says: This is only a declaration.

1 Like

Well, that was a nice education on the different uses of local and global variables; thank you!
Now the program compiles and, even better, it works!