views:

4418

answers:

5

hi i have a bunch of compile time asserts and do something like

CASSERT(isTrue) or CASSERT2(isTrue, prefix_) in GCC i get many "warning '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 then thought i may hack something by having the var automatically used globally inside the same macro but i couldnt think of any hacks.

Does anyone know a solution to hide that warning in GCC?

+2  A: 

How about -Wunused-label ?

DrJokepu
nope but your wrong answer led me to the right one :D
acidzombie24
+5  A: 

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;
acidzombie24
that's exactly how it's also done in gcc source for unused function arguments. +1 :)
Johannes Schaub - litb
I found that using ____attribute____ ((____unused____)) works for GCC 4.x.x
mtasic
+3  A: 

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
+2  A: 

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
+6  A: 

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 are

-Wunused-function
-Wunused-label
-Wunused-parameter
-Wunused-value
-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.

related questions