tags:

views:

159

answers:

7

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.

+1  A: 

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:

  • refactor that macro to #if declare the variable only if it's really used;
  • create another version of the macro that skips the unused variable generation.
  • Better still, avoid using macros that bring issues to the code.
jpinto3912
Not possible in this case. It is a macro that "spits out" the variable and I don't want to use it in this case.
gustaf
There's other cases as well - e.g if you need to provide a callback function to an external API, but you don't really use/care about some of the parameters you'll get warnings
nos
nos: Thank you for not assume I'm totally stupid like many of the comments above.
gustaf
write more precise questions and you'll get more precise answers
Samuel_xL
-1, no substance in the answer. Not even trying to understand the real problem.nos, good comment, +1
Daniel Persson
@Daniel, thanks, because the comments got me false credits, and I was just intending a pun... also, of course I can't understand the real problem from that 2-phrase question. But I'll edit to provide meaningful solution.
jpinto3912
A: 

#pragma unused <variable>

Nathon
Not portable - generates "unknown pragma" warnings on many compilers
Paul R
Paul: That is true and valid, but I'm assuming it doesn't need to be portable since the only reason I can think of to have unused variables is because you have some code commented out for debugging and you still want your (temporary) debugging build to only show warnings you weren't expecting.
Nathon
+1  A: 

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.

Jefromi
+4  A: 

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)) 
Edward Leno
... another write-only header macro is just what we need to get a rather benign warning out of the way.
jpinto3912
A: 

If its used and you are shipping the project, delete it. Worst comment it.

Praveen S
+1  A: 

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.

ShaderOp
+2  A: 

(void) variable might work for some compilers.

Also see: http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/

jamesdlin