cancel
Showing results for 
Search instead for 
Did you mean: 

Avoiding the "variable set but not used" warning

EThom.3
Associate III

This may be such a basic question, that I may be embarrassing myself here. Not even sure if I've selected the best fitting location...

I like to keep a "clean slate" when compiling, that is, no warnings should be present. It makes it easier to see if a problem has occurred, if I don't need to remember how many warnings are to be expected in a certain piece of firmware. Also, if I get too comfortable with a number of warnings being present, something may slip.

Now, in microcontrollers, we sometimes need to read something from a register, without actually needing the value for anything, such as emptying an SPI receive buffer after transmitting. In some cases, I use a variable that already exists, and is in use elsewhere, but this is not always practical. And then I get the "variable set but not used" warning.

I don't want to suppress the warning type altogether, because it does come in handy now and then.

There is the possibility to declare a global variable for this purpose, as the compiler doesn't complain about unused global variables. This just seems a bit "clunky" to me.

It might also be possible to use another (unused) register as the recipient for the unneeded value. That just isn't quite practical if the code is to be ported to a different device or project. Perhaps a read-only register might work? :thinking_face:

Question: What do other people do? Is there a super-clever solution that I just don't know about?

I've searched elsewhere, but it seems that most programmers don't code for microcontrollers, and don't understand why anyone would ever read a variable without using it.

1 ACCEPTED SOLUTION
4 REPLIES 4

I had a hunch that it would be something incredibly simple.

uint8_t Dummy __attribute__ ((unused));

Thanks!

Of course, that's GCC specific.

If you need your code to build with other tools, you'll need something like

#if GCC
uint8_t Dummy __attribute__ ((unused));
#elif KEIL
// something Keil-specific
#else
#error "Unknown compiler"
#endif

 

Makes sense.