2025-01-14 02:37 AM
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.
Solved! Go to Solution.
2025-01-14 02:46 AM
This is why compilers have things like pragmas & attributes.
eg, for GCC:
2025-01-14 02:46 AM
This is why compilers have things like pragmas & attributes.
eg, for GCC:
2025-01-14 03:01 AM
I had a hunch that it would be something incredibly simple.
uint8_t Dummy __attribute__ ((unused));
Thanks!
2025-01-14 03:10 AM
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
2025-01-14 03:12 AM
Makes sense.
2025-01-15 10:58 PM
Hello
i do it by
uint16_t _dummy;
UNUSED (_dummy);
padawan
2025-01-16 12:26 AM
That's quite interesting. It is basically the same as saying
uint16_t _dummy;
(void)_dummy;
I had no idea that (void) could have that effect.
Thanks!
2025-01-16 12:35 AM
Portable solution:
C/C++:
int unused_variable;
(void)unused_variable;
For C++17 and higher and C23 and higher
[[maybe_unused]] int unused_variable;
This one can be used for functions too.
2025-01-17 12:49 AM
The variety of solutions presented here is a stark contrast to the slightly condescending "Why would you ever want to do that???" attitude I found elsewhere by googling.
I like this community.
2025-01-17 03:41 AM
@EThom.3 wrote:"Why would you ever want to do that???" attitude I found elsewhere by googling.
This is the stackoverflow mentality.
If your question is too specific or too generic according to the all-knowing moderators will close the question.
If it is too similar to another question according to the all-knowing moderators they will close your your question.
If it asks for advice, help or opinions instead of a closed objective question the all-knowing moderators close your question.
ST community has a forum and a knowledge base. Stackoverflow is basically a knowledge base pretending to be a forum, but this makes it hard for people who need help with something, but don't know enough to ask the perfect question, because they don't know what they don't know.
In this topic there are many reasons for avoiding warnings. IMHO it's good practice to get 0 warnings from your code, because if you had 9 warnings you may not notice that a 10th warning is serious. But you don't just want to disable warnings globally or per file unless it is absolutely needed. I always enable all warnings and extra warnings. In some cases you want to disable some warnings for some files (third party libraries you cannot modify, for instance). Filescope static functions may not be used if you use conditional compilation, but you might not want to exclude them from your builds to prevent you from noticing errors in them too late.
Unused function arguments cannot be removed without changing the function signature.