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:
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);
}
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 =)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With