57
votes

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.

11
There are legitimate reasons to do this, e.g. take plugin development for a system, that expects your function with a specific signature like void function_name(int par1, char *par2); and you only need to work on par2.Residuum
In that case, write the signature as void function_name(int par1, char*); which is perfectly valid and won't generate warnings.Frank Kusters
@spaceknarf If I do that with gcc 4.7.2 I get error: parameter name omitted.craig65535
I don't think it is valid to leave a parameter unnamed in a C function signature when it's part of the definition. It's acceptable in C++ though.davidA
This is not a duplicate, this is a different question. The other questions ask what ways there are to suppress the warning, this question asks which way we should use.12431234123412341234123

11 Answers

50
votes

(void) variable might work for some compilers.

For C++ code, also see http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/ where Herb Sutter recommends using:

template<class T> void ignore( const T& ) { }

...

ignore(variable);
37
votes

Do not give the variable a name (C++)

void foo(int /*bar*/) {
    ...
}

Tell your compiler using a compiler specific nonstandard mechanism

See individual answers for __attribute__((unused)), various #pragmas and so on. Optionally, wrap a preprocesor macro around it for portability.

Switch the warning off

IDEs can signal unused variables visually (different color, or underline). Having that, compiler warning may be rather useless.

In GCC and Clang, add -Wno-unused-parameter option at the end of the command line (after all options that switch unused parameter warning on, like -Wall, -Wextra).

Add a cast to void

void foo(int bar) {
    (void)bar;
}

as per jamesdlin's answer and http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/

24
votes

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)) 
14
votes

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.

13
votes

You can silence the warning using #pragma

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused"

int unususedVariable = 1;

#pragma clang diagnostic pop

If you are using GCC, use #pragma gcc ...

3
votes

#pragma unused <variable>

3
votes

The cast to a void is the best approach because it shows that you didn't "accidentally" keep the variable in your code - ie: this function might be an instance where you have a table of function pointers that need the same parameter types and return types, but in this particular table entry you are not using the parameter.

That said, if you don't need it, get rid of it. ;)

2
votes

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.

0
votes

Assign it to itself.

void f(int unused) {
    unused = unused;
}

Works in gcc, but clang needs -Wno-self-assign.


Edit: I now think casting to void is the most portable solution: Both gcc and clang understand this, even with full warnings -W{all,extra,pedantic}:

(void)unused;
-9
votes

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.
-10
votes

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