C语言 抑制“未使用的变量 x”警告的最佳方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3417837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to suppress "Unused variable x"-warning
提问by gustaf
What is the best/neatest way to suppress a compiler (in this case gcc) like "Unused variable x"-warning?
抑制编译器(在本例中为 gcc)的最佳/最简洁的方法是“未使用的变量 x”-警告?
I don't want to give any certain flags to gcc to remove all these warnings, just for special cases.
我不想给 gcc 任何特定的标志来删除所有这些警告,只是为了特殊情况。
采纳答案by Edward Leno
Found an article http://sourcefrog.net/weblog/software/languages/C/unused.htmlthat explains UNUSED. Interesting that the author also mangles the unused variable name so you can't inadvertently use it in the future.
找到了一篇解释 UNUSED的文章http://sourcefrog.net/weblog/software/languages/C/unused.html。有趣的是,作者还对未使用的变量名进行了修改,以免日后无意中使用它。
Excerpt:
摘抄:
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
void dcc_mon_siginfo_handler(int UNUSED(whatsig))
回答by jamesdlin
(void) variablemight work for some compilers.
(void) variable可能适用于某些编译器。
For C++ code, also see http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/where Herb Sutter recommends using:
对于 C++ 代码,另请参阅http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/其中 Herb Sutter 建议使用:
template<class T> void ignore( const T& ) { }
...
ignore(variable);
回答by user7610
Do not give the variable a name (C++)
不要给变量一个名字(C++)
void foo(int /*bar*/) {
...
}
Tell your compiler using a compiler specific nonstandard mechanism
使用特定于编译器的非标准机制告诉您的编译器
See individual answers for __attribute__((unused)), various #pragmas and so on. Optionally, wrap a preprocesor macro around it for portability.
请参阅__attribute__((unused))、各种#pragmas 等的个别答案。或者,为了可移植性,在它周围包裹一个预处理器宏。
Switch the warning off
关闭警告
IDEs can signal unused variables visually (different color, or underline). Having that, compiler warning may be rather useless.
IDE 可以直观地表示未使用的变量(不同颜色或下划线)。有了这个,编译器警告可能毫无用处。
In GCC and Clang, add -Wno-unused-parameteroption at the end of the command line (after all options that switch unused parameter warning on, like -Wall, -Wextra).
在 GCC 和 Clang 中,-Wno-unused-parameter在命令行末尾添加选项(在所有打开未使用参数警告的选项之后,例如-Wall, -Wextra)。
Add a cast to void
向 void 添加演员表
void foo(int bar) {
(void)bar;
}
as per jamesdlin's answer and http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/
根据 jamesdlin 的回答和http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/
回答by Cascabel
If this is really what you want, you could use the unused attribute (gcc only), something like:
如果这确实是您想要的,您可以使用未使用的属性(仅限 gcc),例如:
void foo(int __attribute__((__unused__)) bar) {
...
}
Not just for function parameters, of course, but that's the most common use case, since it might be a callback function for an API where you don't actually need all the input.
当然,不仅用于函数参数,而且这是最常见的用例,因为它可能是 API 的回调函数,您实际上并不需要所有输入。
Additionally, GLib has a G_GNUC_UNUSED macro which I believe expands to that attribute.
此外,GLib 有一个 G_GNUC_UNUSED 宏,我相信它可以扩展到该属性。
回答by James Webster
You can silence the warning using #pragma
您可以使用以下命令使警告静音 #pragma
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused"
int unususedVariable = 1;
#pragma clang diagnostic pop
If you are using GCC, use #pragma gcc ...
如果您使用的是 GCC,请使用 #pragma gcc ...
回答by nmichaels
#pragma unused <variable>
#pragma unused <variable>
回答by Mike Stroven
The cast to a void is the best approach because it shows that you didn't "accidentally" keep the variable in your code - ie: this function might be an instance where you have a table of function pointers that need the same parameter types and return types, but in this particular table entry you are not using the parameter.
强制转换为 void 是最好的方法,因为它表明您没有“意外地”在代码中保留变量 - 即:此函数可能是一个实例,其中您有一个需要相同参数类型的函数指针表,并且返回类型,但在这个特定的表条目中,您没有使用参数。
That said, if you don't need it, get rid of it. ;)
也就是说,如果您不需要它,请摆脱它。;)
回答by Mhmmd
It's a very hackish solution, but did you try simply assigning the variable to itself? I think that should fool most compilers into thinking that the variable is used. Should be quite portable too.
这是一个非常黑客的解决方案,但是您是否尝试简单地将变量分配给自身?我认为这应该让大多数编译器误以为使用了该变量。应该也很便携。
回答by user2394284
Assign it to itself.
将其分配给自己。
void f(int unused) {
unused = unused;
}
Works in gcc, but clang needs -Wno-self-assign.
在 gcc 中工作,但 clang 需要 -Wno-self-assign。
Edit: I now think casting to void is the most portable solution: Both gcc and clang understand this, even with full warnings -W{all,extra,pedantic}:
编辑:我现在认为强制转换为 void 是最便携的解决方案:gcc 和 clang 都明白这一点,即使有完整的警告-W{all,extra,pedantic}:
(void)unused;
回答by jpinto3912
Delete the unused variable declaration from the code. (pun intended)
从代码中删除未使用的变量声明。(双关语)
(What??? it's what I do: point that the obvious is most likely the best solution.)
(什么???这就是我所做的:指出显而易见的最有可能是最好的解决方案。)
Now, from comments on other answers, apparently it's garbage generated from macros. Well, that's a pleonasm.
现在,从对其他答案的评论来看,显然它是由宏生成的垃圾。嗯,这是一个pleonasm。
Solutions:
解决方案:
- refactor that macro to #if declare the variable only if it's really used;
- create another version of the macro that skips the unused variable generation.
- Better still, avoid using macros that bring issues to the code.
- 将该宏重构为 #if 仅在真正使用该变量时才声明该变量;
- 创建另一个版本的宏,跳过未使用的变量生成。
- 更好的是,避免使用给代码带来问题的宏。

