可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
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;
回答3:
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);
回答4:
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()
{
}
回答5:
#define UNUSED_VAR __attribute__ ((unused))
for any variable just use the above macro before its type for example:
UNUSED_VAR int a = 2;
回答6:
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
回答7:
How about -Wunused-label
?
回答8:
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