How can I hide “specific but not used” warnings in GCC?

I have a bunch of compile time, for example:

CASSERT(isTrue) or CASSERT2(isTrue, prefix_) 

When compiling with GCC, I get a lot of warnings like 'prefix_LineNumber' defined but not used . Is there a way I can hide warnings for compile time? I had no luck in the GCC documentation. I thought that perhaps var would automatically be used globally within the same macro, but I could not figure out how to do this.

Does anyone know how to hide this warning in GCC?

+58
gcc compiler-warnings warnings
Dec 22 '08 at 13:02
source share
8 answers

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

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 them has a corresponding negative form with "no-" inserted after W, which disables the warning (if it was enabled, for example, -Wall). So in your case you should use

 -Wno-unused-function 

Of course, this works for all code, not just compilation. For the behavior specific to a function, see Functional Attributes .

+93
Mar 04 '09 at 11:17
source share

GCC solution that does not conflict 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; 
+44
Dec 22 '08 at 13:45
source share

You can create a null statement and invalidate the result. This is portable for compilers, and gcc will not give you any warnings, even if -Wall and -Wextra . 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); 
+31
Dec 27 '08 at 2:12
source share

This is one of the most anonymous warnings, although I have found it helpful (sometimes) to check for dead code. But I usually have static functions for debugging or functions that may be useful sometime in the future or that are only used temporarily, and I want to store them in code.

Fortunately, this warning does not care about the built-in functions.

 inline static foo() { } 
+28
May 12 '11 at 10:09
source share
 #define UNUSED_VAR __attribute__ ((unused)) 

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

 UNUSED_VAR int a = 2; 
+6
Mar 05 '15 at 14:37
source share

It's hard to answer without knowing the details of your static static macros. Perhaps you can switch to another macro to avoid this problem? You could either add the “unused” attribute to the macro as suggested, or use a different form of CASSERT ().

Below are descriptions of several 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

+3
Dec 27 '08 at 1:33
source share

What about -Wunused-label ?

+2
Dec 22 '08 at 13:15
source share

Wrap these functions with the following directives. All code that will be placed between push and pop will not warn you of unused functions. All other code (external push and pop) will not be affected.

 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" ... your code #pragma GCC diagnostic pop 
0
Dec 10 '18 at 16:47
source share



All Articles