75

I have a bunch of compile time asserts, such as:

CASSERT(isTrue) or CASSERT2(isTrue, prefix_)

When compiling with GCC I get many warnings like 'prefix_LineNumber' defined but not used. Is there a way I can hide warnings for compile time asserts? I had no luck searching the GCC documentation. I thought I might have the var automatically used globally inside the same macro but I couldn't think of any way to do it.

Does anyone know of a way to hide that warning in GCC?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40

9 Answers9

113

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.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • 2
    Can this be set per file? – Danijel May 04 '17 at 08:38
  • The problem with such a flag is that it going to hide other variables which may indeed be unused and should be removed from your software. So it's not a good solution. However, marking one specific variable as willingly unused is much better (i.e. a variable used to run some code when your library is loaded, which is a neat C++ feature.) – Alexis Wilke Aug 09 '19 at 05:09
55

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;
47

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()
{
}
taskinoor
  • 45,586
  • 12
  • 116
  • 142
mastermemorex
  • 471
  • 4
  • 2
34

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);
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 1
    Oddly, I still get warnings. For example, when I call system() and don't care about the return: `(void)system(cmd.c_str());` I don't understand why this should generate a warning. – BobDoolittle Oct 31 '14 at 17:24
  • @BobDoolittle: Huh, that's interesting, I haven't seen that behavior before. What compiler and what version of that compiler are you using? – Adam Rosenfield Oct 31 '14 at 20:11
  • gcc 4.8.2 on Ubuntu 14.04. I am compiling all my source with -Wunused-result, and looking for a way to explicitly suppress the warning for those places where it's verified that the behavior is correct. I've always used a cast to (void) for that in the past, but not working here. – BobDoolittle Oct 31 '14 at 21:29
  • @BobDoolittle: I'm unable to reproduce that behavior using the same gcc 4.8.2 on Ubuntu 14.04. Can you post/link an [SSCCE](http://sscce.org/) along with the exact command line arguments you're passing to the compiler? – Adam Rosenfield Nov 01 '14 at 04:53
10
#define UNUSED_VAR     __attribute__ ((unused))

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

UNUSED_VAR int a = 2;
The joker
  • 189
  • 1
  • 6
  • 2
    We have adopted this for out project. BUT, only for cases where the compiler is mistaken in its warnings. For instance, we have some variables which are only ever accessed by pointer, so the compiler believes them to be unused. It is acceptable to supress warnings for such, but truly unused variables must be removed from our code. – Mawg says reinstate Monica Jun 08 '16 at 08:01
10

Wrap this functions by the following directives All the code that will be placed between push and pop will not warn you about unused functions. All the rest of the code (outside push and pop) will not be affected.

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"

... your code

#pragma GCC diagnostic pop
Sasha Itin
  • 161
  • 1
  • 6
3

This is hard to answer without knowing the details of your static assert macros. Perhaps you could change to a different macro to avoid this problem? You could either add the 'unused' attribute to the macro as was suggested, or you could use a different form of CASSERT().

Here are descriptions of a few alternatives:

http://www.jaggersoft.com/pubs/CVu11_3.html

http://blog.kowalczyk.info/kb/compile-time-asserts-in-c.html

http://www.pixelbeat.org/programming/gcc/static_assert.html

Nathan Kurz
  • 1,649
  • 1
  • 14
  • 28
2

How about -Wunused-label ?

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
0

As of C++ 17 you can use [[maybe_unused]]. For example,

[[maybe_unused]] int foo = 42;

which has the same effect as the old(er) __attribute__((unused)).

Carlo Wood
  • 5,648
  • 2
  • 35
  • 47