How to Disable the Unused Variable Warnings Coming Out of Gcc in 3Rd Party Code I Do Not Wish to Edit

How do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?

The -Wno-unused-variable switch usually does the trick. However, that is a very useful warning indeed if you care about these things in your project. It becomes annoying when GCC starts to warn you about things not in your code though.

I would recommend you keeping the warning on, but use -isystem instead of -I for include directories of third-party projects. That flag tells GCC not to warn you about the stuff you have no control over.

For example, instead of -IC:\\boost_1_52_0, say -isystem C:\\boost_1_52_0.

Hope it helps. Good Luck!

How can I suppress unused parameter warnings in C?

I usually write a macro like this:

#define UNUSED(x) (void)(x)

You can use this macro for all your unused parameters. (Note that this works on any compiler.)

For example:

void f(int x) {
UNUSED(x);
...
}

How do I best silence a warning about unused variables?

You can put it in "(void)var;" expression (does nothing) so that a compiler sees it is used. This is portable between compilers.

E.g.

void foo(int param1, int param2)
{
(void)param2;
bar(param1);
}

Or,

#define UNUSED(expr) do { (void)(expr); } while (0)
...

void foo(int param1, int param2)
{
UNUSED(param2);
bar(param1);
}

What is the best way to suppress A Unused variable x warning?

I found an article, http://sourcefrog.net/weblog/software/languages/C/unused.html, that explains UNUSED. It is 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))

Disable unused variable warning from Boost graph library within CMake

If your compiler supports the pragma disable warning #number then you can surround the BGL code with pragma disable ... and pragma enable ...

gcc warns about unused RAII variable

GCC is smart enough to detect if the definition of a variable invokes a constructor call. In your case, ensuring that a constructor is indeed invoked (even an empty one) will mark the variable definition as having a side-effect and ensure that you will not get a warning anymore.

This behavior holds true even for ancient versions of GCC.

gcc -O0 still optimizes out unused code. Is there a compile flag to change that?

GCC doesn't "optimize out" anything here. It just doesn't generate useless code. It seems to a very common illusion that there's some pure form of code that the compiler should generate and any changes to that are an "optimization". There is no such thing.

The compiler creates some data structure that represents what the code means, then it applies some transformations on that data structure and from that it generates assembler that then gets compiled down to instructions. If you compile without "optimizations" it just means that the compiler will only do the least effort possible to generate code.

In this case, the whole statement is useless because it doesn't do anything and is thrown away immediately (after expanding the inlines and what the builtins mean it is equivalent to writing a/b;, the difference is that writing a/b; will emit a warning about statement with no effect while the builtins probably aren't handled by the same warnings). This is not an optimization, the compiler would actually have to expend extra effort to invent meaning to a meaningless statement, then fake a temporary variable to store the result of this statement to then throw it away.

What you're looking for is not flags to disable optimizations, but pessimization flags. I don't think any compiler developers waste time implementing such flags. Other than maybe as an April fools joke.



Related Topics



Leave a reply



Submit