What is the best/neatest way to suppress a compiler (in this case gcc) like "Unused variable x"-warning?
I don't want to give any certain flags to gcc to remove all these warnings, just for special cases.
What is the best/neatest way to suppress a compiler (in this case gcc) like "Unused variable x"-warning?
I don't want to give any certain flags to gcc to remove all these warnings, just for special cases.
Delete the unused variable declaration from the code. (pun intended)
(What??? it's what I do: point that the obvious is most likely the best solution.)
Now, from comments on other answers, apparently it's garbage generated from macros. Well, that's a pleonasm.
Solutions:
If this is really what you want, you could use the unused attribute (gcc only), something like:
void foo(int __attribute__((__unused__)) bar) {
...
}
Not just for function parameters, of course, but that's the most common use case, since it might be a callback function for an API where you don't actually need all the input.
Additionally, GLib has a G_GNUC_UNUSED macro which I believe expands to that attribute.
Found an article http://sourcefrog.net/weblog/software/languages/C/unused.html that explains UNUSED. Interesting that the author also mangles the unused variable name so you can't inadvertently use it in the future.
Excerpt:
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
void dcc_mon_siginfo_handler(int UNUSED(whatsig))
If its used and you are shipping the project, delete it. Worst comment it.
It's a very hackish solution, but did you try simply assigning the variable to itself? I think that should fool most compilers into thinking that the variable is used. Should be quite portable too.
(void) variable
might work for some compilers.
Also see: http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/