5

How can I disable the following warning in C++ in minGW?

warning: unused variable 'x' [-Wunused-variable]

In Eclipse CDT, I can't locate the warning number:

../src/subfolder/ClassTwo.cpp:20:8: warning: unused variable 'x' [-Wunused-variable]

I tried doing this:

#pragma warning(push)
#pragma warning(disable: ?) //which number?
#include "subfolder/ClassTwo.h"
#pragma warning(pop)

But it didn't work.

My questions:

  1. How can I get the warning number of this in Eclipse CDT?
  2. How should the pragma directive be written?
jiafu
  • 6,338
  • 12
  • 49
  • 73
  • 3
    If it's a function parameter, just leave it unnamed. Assuming you use GCC, it's more like `#pragma gcc diagnostic` something or other, though. – chris May 14 '13 at 15:37
  • 1
    Just fix the code, and the warning will go away by itself... -- by the way, what makes you think that disabling the warnings in the header will help with the warning that appears to happen in the .cpp – David Rodríguez - dribeas May 14 '13 at 15:41
  • guys, it may be a 3rd party header which was built with more relaxed conventions. if not, then yeah - just fix it. – justin May 14 '13 at 15:42
  • @DavidRodríguez-dribeas, To be fair, I get these a lot if I don't leave things unnamed when using the Windows API with its callbacks that need specific parameters, even if unused. – chris May 14 '13 at 15:42
  • 1
    @chris: so leave the arguments unnamed, or silence the warning by *casting* to void: `void foo(int ignored) { (void)ignored; .... }` – David Rodríguez - dribeas May 14 '13 at 16:39
  • @DavidRodríguez-dribeas, Of course. It doesn't necessarily mean the code is *broken* otherwise if they have to be there, but you don't use them. – chris May 14 '13 at 16:39
  • @chris: Warnings are there to help diagnose issues. If you disable them at the compiler level they cannot help you. The alternative, when you know that it is a false positive is fixing the particular warning. Leaving the argument unnamed, or casting to void tell other maintainers that you are aware that the object is not used otherwise, while it lets the compiler warn if you introduce new issues that might or not be intentional. Of course there are two ways of building without warnings: fixing them, or disabling warnings in the compiler. I'd rather take the first approach. – David Rodríguez - dribeas May 14 '13 at 19:21
  • @DavidRodríguez-dribeas, I completely agree. I was never arguing for disabling warnings, just that getting warnings doesn't necessarily mean your code is broken. It just requires a workaround, one way or another, to assure the compiler that's what you really wanted. – chris May 14 '13 at 19:24

3 Answers3

6

Since it's NEARLY always easy to fix "unused variable" warnings, I'd very much prefer to fix the actual code than try to patch it up with pragmas (which may hide other, future errors too - for example you add a new function:

 int foo(int x, int y)
 {
      return x * x;
 }

Oops, that's a typo, it's supposed to be return x * y; - a warning would give you indication that this is the case.

Unused parameters, as someone mentioned, are dealt with by removing the name of the parameter:

 int foo(int x, int)  // Second parameter, y is not used
 {
      return x * x; 
 }

If it's a local variable, then you can use (void)y (perhaps in aa macro) to "fake use it":

 int bar(int x)
 {
    int y;    // Not used. 
    (void)y;
 }

or

 #define NOT_USED(x) (void)(x)

      int bar(int x)
 {
    int y;    // Not used. 
    NOT_USED(y);
 }
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • thanks, but I only want to try the pragma's feature, not write the right code self – jiafu May 14 '13 at 15:49
  • What's Microsoft's? `UNREFERENCED_PARAMETER`? – chris May 14 '13 at 15:50
  • Yes, that would appear to be the case. I've also seen "UNUSED(x)" as a macro for this. – Mats Petersson May 14 '13 at 15:58
  • I'd recommend using `#define UNUSED(x) (void)sizeof(x)`, I can't quite remember the exact code but I had some really weird random crashes from casting some void pointers to void, or maybe it was pointer to void pointer or something like that. – Edward A May 14 '13 at 16:06
  • I have used `#define UNUSED(x) (void)(x)` many times, including when pointers are involved. As long as you pass in the actual variable, and not, for example `*x`, I'd say it's a bug in the compiler if it crashes. – Mats Petersson May 14 '13 at 16:08
  • It could be that, I was using some experimental version of mingw 4.8.0 when it just came out. Unfortunately I hadn't thought of saving the bit of code, but just to be safe I've stuck to void casting the sizeof since then, and I'd like to think that the compiler optimises it out anyway. – Edward A May 14 '13 at 16:12
  • @EdwardA, The `sizeof` part is done at compile-time anyway, so it won't hurt anyone. The whole statement is almost certainly going to be optimized out as well. – chris May 14 '13 at 19:26
3

It looks like the output from clang. You can achieve the same using clang using the approach outlined here: http://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
#include "subfolder/ClassTwo.h"    
#pragma clang diagnostic pop

If that's your source file, then just fix the warning.

For GCC, you can use this: Selectively disable GCC warnings for only part of a translation unit?

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#include "subfolder/ClassTwo.h"
#pragma GCC diagnostic pop

Of course, that will leave you with a good amount of pragma noise -- debatable if that is a bad thing =)

Community
  • 1
  • 1
justin
  • 104,054
  • 14
  • 179
  • 226
  • g++ -I"C:\c+\gtest-1.6.0\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Test.d" -MT"src/Test.d" -o "src/Test.o" "../src/Test.cpp" ../src/Test.cpp:1:0: warning: ignoring #pragma warning [-Wunknown-pragmas] ../src/Test.cpp:2:0: warning: ignoring #pragma warning [-Wunknown-pragmas] ../src/Test.cpp:3:0: warning: ignoring #pragma warning [-Wunknown-pragmas] ../src/Test.cpp:5:0: warning: ignoring #pragma warning [-Wunknown-pragmas] – jiafu May 14 '13 at 15:44
  • @jiafu ok - linked the GCC approach and added a way for you to fix the warning. – justin May 14 '13 at 15:49
3

You seem to be using Microsoft C++ style pragma syntax with GCC compiler. The concept of "warning number" (at least in that format) is also Microsoft C++ specific. In other words, this should not work in GCC.

There's no standard syntax for disabling/enabling warnings, so each compiler will use its own. That means that there's no way to do it "in C++" (quoting your title). There are only ways to do it in each specific compiler. You have to consult your compiler docs for that.

In GCC you should be able to do it through command-line options -Wno-unused-variable do disable all such warnings for the entire translation unit. Also, in GCC you can actually selectively mark variables as unused through __attribute__((unused)) to suppress warnings for that specific variable.

http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes

parvus
  • 5,706
  • 6
  • 36
  • 62
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765