C语言 警告:变量设置但未使用 [-Wunused-but-set-variable]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7115088/
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
warning: variable set but not used [-Wunused-but-set-variable]
提问by thetna
I have been getting following warning while compiling the C source code in the gcc 4.6.1.
在gcc 4.6.1.
warning: variable set but not used [-Wunused-but-set-variable]
I refered to this link Wunusedbut could get exactly what is causing this warning.Would anybody tell me in more details what is causing this warning and how can We get rid of it?
我参考了这个链接Wunused但可以确切地知道是什么导致了这个警告。有人能更详细地告诉我是什么导致了这个警告,我们如何摆脱它?
[EDIT] I have a following snippet of code. The compile shows the above mentioned warning. Could you please suggest me how can correct it?
[编辑] 我有以下代码片段。编译显示上述警告。你能建议我如何纠正它吗?
test_function(){
BOOL BoolTest;
BoolTest = test_fucntion2();
#ifdef CHECK
if (!BoolTest) {
misc_StartErrorReport();
misc_ErrorReport("\n test_function2: Input not indexed.\n");
misc_FinishErrorReport();
}
#endif
//
BoolTest is no more used below it.
// }
采纳答案by caf
You need to include the preprocessor guards around the declaration and initialisation of BoolTest:
您需要在以下声明和初始化周围包含预处理器保护BoolTest:
test_function()
{
#ifdef CHECK
BOOL BoolTest = test_function2();
#else
test_function2();
#endif
#ifdef CHECK
if (!BoolTest) {
misc_StartErrorReport();
misc_ErrorReport("\n test_function2: Input not indexed.\n");
misc_FinishErrorReport();
}
#endif
(this assumes that you still want to call test_function2()even if CHECKis not defined, presumably for its side-effects - if not, then you don't need the #elsesection and you can combine the two #ifdefblocks into one).
(这假设您仍然想要调用test_function2()即使CHECK未定义,大概是因为它的副作用 - 如果没有,那么您不需要该#else部分,您可以将两个#ifdef块合并为一个)。
回答by pmg
Setting a variable is assigning it a value (maybe implicitly)
设置变量就是为其赋值(可能是隐式的)
int main(void) {
int local1, local2;
local1 = 0; /* local1 set to 0 */
local2 = 0; /* local2 set to 0 */
return 0;
}
In the program above, both variables were set to a value but they weren't used. If I replace the second line with
在上面的程序中,两个变量都设置了一个值,但没有使用它们。如果我用
int local2 = local1;
now I have used the local1variable -- and the warnings should be only 1.
现在我已经使用了local1变量——警告应该只有 1。
To get rid of the warning, delete the assignment from your code. This may, in turn create other warnings ... :)
要消除警告,请从代码中删除分配。这可能反过来产生其他警告...... :)
回答by RustyTheBoyRobot
It means that you assign a value to a variable, but then you never read that value later in your code (hence the verbage, "set but not used"). For example:
这意味着您为变量分配了一个值,但之后您永远不会在代码中读取该值(因此,“设置但未使用”)。例如:
int useful = 10;
int useless = 3;
if (useful) {
//Do stuff
}
Notice that you give both usefuland uselessvalues, but you only read the value in useful.
Usually, when I get this message it means that I forgot about a variable or found a way to inline a statement that no longer needs that variable.
请注意,您同时给出了useful和useless值,但您只读取了 中的值useful。通常,当我收到此消息时,这意味着我忘记了某个变量或找到了一种方法来内联不再需要该变量的语句。
回答by Carlo Wood
With g++ 7.x and higher and clang++ 4.x and higher (using c++11 or higher), as well as with Visual Studio 2017 version 15.3 and later (available with /std:c++17), you can use the standarized [[maybe_unused]] attribute.
使用 g++ 7.x 及更高版本和 clang++ 4.x 及更高版本(使用 c++11 或更高版本),以及 Visual Studio 2017 版本 15.3 及更高版本(可用于 /std:c++17),您可以使用标准化的 [[maybe_unused]] 属性。
For example,
例如,
int main()
{
int x [[maybe_unused]] = 5;
}
will not give a warning, not even with -Wunused-variable and the likes.
不会发出警告,甚至不会发出 -Wunused-variable 之类的警告。
回答by mahmood
You have not used BoolTest. You can see there is no difference between your code and
您还没有使用过 BoolTest。您可以看到您的代码和
test_function(){
#ifdef CHECK
if (!test_fucntion2()) {

