Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable Unused Variable warnings in Eclipse in minGW?

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?
like image 540
jiafu Avatar asked May 14 '13 15:05

jiafu


3 Answers

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);
 }
like image 79
Mats Petersson Avatar answered Sep 17 '22 05:09

Mats Petersson


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 =)

like image 38
justin Avatar answered Sep 19 '22 05:09

justin


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

like image 26
AnT Avatar answered Sep 21 '22 05:09

AnT