在GCC中编译时使用`-Wextra`标志的缺点 [英] Disadvantages of using the `-Wextra` flag when compiling in GCC

查看:4945
本文介绍了在GCC中编译时使用`-Wextra`标志的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道应该始终使用 -Wall -Wextra 进行编译,因为它们会启用警告并帮助我们以了解我们的错误,如果有的话。



我读过 -Wextra 编译器标志不推荐因为它太冗长而带有很多误报。



我在阅读这本书时感到非常惊讶。所以我开始使用Google搜索,但是我没有得到任何答案,因为所有的搜索结果显示什么是 -Wextra 标志?。



所以,我的问题是 $ b


  • 其中所有情况都是 - Wextra 标志发出不必要的警告?

  • 是否有可能停止 -Wextra 标志启用另一个导致GCC发出这些类型警告的标志?


解决方案

-Wextra 警告是相应的警告具有以下属性(或多或少):


  1. 他们产生误报。


其结果是,许多项目切换在 -Wall -Wextra 放弃试图避免al l警告。因此,当你编译这样一个项目时,你会看到成百上千的警告,几乎所有关于合法代码的警告。在无尽的警告流中,你应该看到的一个警告不被忽视。更糟糕的是:正常编译吐出这么多的警告会让你对于避免新代码引入的新警告变得sl fact不安。一旦项目达到几十个警告,战斗通常会结束;它会需要一个英勇的努力,使警告计数回到零。



下面是一些完美合法的代码的例子,吠叫在 -Wextra

  void myClass_destruct(MyClass * me){} 

这是一个析构函数,它是空的。但是,它应该只是简单地在适当的位置(子类析构函数)调用它,以便将需要清理的东西添加到 MyClass 中。但是, -Wextra 会在未使用的 me 参数中吠叫。这迫使程序员编写如下代码:

  void myClass_destruct(MyClass * me){
(void)me; //关闭编译器关于未使用的参数吠叫
}

这是普通的噪音。编写这样的代码让人讨厌。但是,为了在 -Wextra 制度下实现零警告计数,需要将该噪声添加到代码中。因此,您可以选择以下三种中的任何两种:


  1. 清洁代码


  2. 零警告次数


  3. -Wextra 警告


明智地选择您想要放弃哪一个,就不会得到全部三个。


I know that one should always compile with both -Wall and -Wextra as they enable warnings and help us to understand our mistake, if any.

I've read that the -Wextra compiler flag is not recommended to use because it is too verbose with a lot of false positives.

I was quite surprised on reading this. So I started googling about it but I didn't get any answer as all the search results showed was "what does the -Wextra flag do?".

So, my questions are

  • In which all situations does the -Wextra flag emit uneccessary warnings?
  • Is it possible to stop the -Wextra flag from enabling the other flags that cause GCC from emitting these types of warnings?

解决方案

The point about the usefulness of -Wextra warnings is that the corresponding warnings have these properties (more or less):

  1. They generate false positives.

  2. The false positives are relatively frequent.

  3. Adapting the code to avoid false positives is a nuisance.

The consequence is, that many projects that switch on -Wall -Wextra give up trying to avoid all the warnings. Consequently, when you compile such a project, you see hundreds and hundreds of warnings, almost all about legit code. And the one warning that you should have seen slips unnoticed in the endless warning stream. Worse: the fact that a normal compilation spits out so many warnings makes you get sloppy about avoiding the new warnings that your new code introduces. Once a project reaches a few tens of warnings, the fight is usually over; it will require a heroic effort to bring the warning count back to zero.

Here is an example of some perfectly legitimate code that is barked at by -Wextra:

void myClass_destruct(MyClass* me) {}

It's a destructor and it's empty. Yet, it should be there simply to facilitate calling it at the appropriate points (subclass destructors), so that it is easy to add stuff to MyClass that needs cleanup. However, -Wextra will bark at the unused me parameter. This forces programmers to write code like this:

void myClass_destruct(MyClass* me) {
    (void)me;    //shut up the compiler barking about the unused parameter
}

This is plain noise. And it gets annoying to write such code. However, in order to achieve a zero warning count under a -Wextra regime, this noise needs to be added to the code. So, you can pick any two of these three:

  1. Clean code

  2. Zero warning count

  3. -Wextra warnings

Choose wisely which one you want to drop, you won't get all three.

这篇关于在GCC中编译时使用`-Wextra`标志的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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