g ++-5.1.1仅在使用优化标志时警告未使用的变量 [英] g++-5.1.1 warns about unused variable only when optimization flag is used

查看:126
本文介绍了g ++-5.1.1仅在使用优化标志时警告未使用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个大型项目中,仅在构建发行版(使用优化标志)时,我才从g ++-5.1.1
得到一些编译器警告,而在构建调试版时,则没有
。这会禁用大多数编译器优化)。
我已将问题缩小为下面列出的最小示例,并使用命令
重现了该问题。如果我使用g ++-4.8.4,则不会发生此问题。
是g ++-5.1.1中的错误吗?或者,此代码是在做某些合理错误的事情并应发出警告?为什么在代码中列出的最后三种情况下不产生任何警告(请参见底部的编辑以获取一些说明)?



对于那些感兴趣的人,这里是错误报告在GCC的Bugzilla中。

  / * 

此代码抱怨仅当优化
标志与g ++-5.1.1一起使用而g ++-4.8.4在$ b $中不产生任何警告时,变量容器才被使用。 b两种情况。尝试使用以下命令:

$ g ++ --version
g ++(GCC)5.1.1 20150618(Red Hat 5.1.1-4)
版权所有(C )2015 Free Software Foundation,Inc.
这是免费软件;请参阅复制条件的来源。没有
保修;甚至不是出于适销性或针对特定目的的适用性。

$ g ++ -c -std = c ++ 11 -Wall -g -O0 test_warnings.cpp

$ g ++ -c -std = c ++ 11 -Wall- O3 test_warnings.cpp
test_warnings.cpp:34:27:警告:已定义容器,但未使用[-Wunused-variable]
const std :: array< Item,5>容器 {} ;
^
* /
#include< array>
结构项
{
int itemValue_ {0};
Item(){};
};

//
//如果执行以下任一操作,警告将消失:
//
//-注释掉Item的构造函数。
//-从下一行删除 const(即,使容器为非const)。
//-从下一行删除 {}(即删除初始化程序列表)。
//
const std :: array< Item,5>容器 {} ;
//
//这些行不会产生任何警告:
//
const std :: array< Item,5> container_1;
std :: array< Item,5> container_2;
std :: array< Item,5> container_3 {};

编辑:正如Ryan Haining在评论中提到的, container_2 container_3 将具有 extern 链接,并且编译器无法警告其用法。

解决方案

如果我们查看-Wunused-variable 它说(强调我的):


此选项表示-Wunused-const-variable适用于C,但不适用于C ++


,如果我们查看 -Wunused-const-variable ,它会说:


除常量
声明外,未使用常量静态变量时警告。对于C,此警告是通过-Wunused-variable启用的,而对于C ++,则是
启用的。 在C ++中,这通常不是错误,因为const
变量在C ++中代替了#defines。


,我们可以在 gcc的修订版中看到此警告消失了。 p>

此gcc错误报告:- Wunused变量会忽略未使用的const初始化变量。尽管它是针对C语言的,但也讨论了C ++的情况。


In a large project, I've been getting some compiler warnings from g++-5.1.1 only when building the release version (which uses optimization flags) but not while building the debug version (which disables most compiler optimization). I've narrowed down the problem to a minimal example listed below with commands to reproduce the problem. The problem does not occur if I use g++-4.8.4. Is this a bug in g++-5.1.1? Or, is this code doing something that is legitimately wrong and warrants that warning? Why doesn't it produce any warnings for the last three cases listed in the code (see edit at the bottom for some explanation)?

For those who are interested, here is the bug report in GCC's Bugzilla.

/*

This code complains that the variable 'container' is unused only if optimization
flag is used with g++-5.1.1 while g++-4.8.4 does not produce any warnings in
either case.  Here are the commands to try it out:

$ g++ --version
g++ (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -c -std=c++11 -Wall  -g -O0 test_warnings.cpp

$ g++ -c -std=c++11 -Wall  -O3 test_warnings.cpp
test_warnings.cpp:34:27: warning: ‘container’ defined but not used [-Wunused-variable]
 const std::array<Item, 5> container {} ;
                            ^
*/
#include <array>
struct Item 
{
    int itemValue_ {0} ;
    Item() {} ;
} ;

//
// The warning will go away if you do any one of the following:
// 
// - Comment out the constructor for Item.
// - Remove 'const' from the next line (i.e. make container non-const).
// - Remove '{}' from the next line (i.e. remove initializer list).
//
const std::array<Item, 5> container {} ;
//
// These lines do not produce any warnings:
//
const std::array<Item, 5> container_1 ;
std::array<Item, 5> container_2 ;
std::array<Item, 5> container_3 {} ;

Edit: As mentioned by Ryan Haining in the comments, container_2 and container_3 will have extern linkage and the compiler has no way of warning about their usage.

解决方案

This looks like a bug, if we look at the documentation for -Wunused-variable it says (emphasis mine):

This option implies -Wunused-const-variable for C, but not for C++

and if we look at -Wunused-const-variable it says:

Warn whenever a constant static variable is unused aside from its declaration. This warning is enabled by -Wunused-variable for C, but not for C++. In C++ this is normally not an error since const variables take the place of #defines in C++.

and we can see this warning goes away in the head revision of gcc.

This gcc bug report: -Wunused-variable ignores unused const initialised variables is also relevant. Although it is against C the C++ case is also discussed.

这篇关于g ++-5.1.1仅在使用优化标志时警告未使用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆