Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide "defined but not used" warnings in GCC?

Just saw this thread while searching for solutions to this problem. I post here for completeness the solution I found...

The GCC compiler flags that control unused warnings include:

-Wunused-function
-Wunused-label
-Wunused-parameter
-Wunused-value
-Wunused-variable
-Wunused (=all of the above)

Each of these has a corresponding negative form with "no-" inserted after the W which turns off the warning (in case it was turned on by -Wall, for example). Thus, in your case you should use

-Wno-unused-function

Of course this works for the whole code, not just compile-time asserts. For function-specific behaviour, have a look at Function attributes.


Solution for GCC not causing conflicts with other compilers

#ifdef __GNUC__
#define VARIABLE_IS_NOT_USED __attribute__ ((unused))
#else
#define VARIABLE_IS_NOT_USED
#endif

int VARIABLE_IS_NOT_USED your_variable;

This is one of the most anoying warnings, although I undestand that it may useful (sometimes) to check dead code. But I usually have static functions for debugging, or functions that maybe useful sometime in the future, or that are only used temporaly, and I want to keep them in the code.

Fortunately this warning does not care about inline functions.

inline static foo()
{
}

You can create a null statement and cast the result to void. This is portable across compilers, and gcc will not give you any warnings, even with -Wall and -Wextra enabled. For example:

int var;    // var is not used
(void)var;  // null statement, cast to void -- suppresses warning

A common technique is to create a macro for this:

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

int var;
UNUSED(var);

#define UNUSED_VAR     __attribute__ ((unused))

for any variable just use the above macro before its type for example:

UNUSED_VAR int a = 2;