如何打开(字面上)所有的GCC的警告? [英] How to turn on (literally) ALL of GCC's warnings?

查看:243
本文介绍了如何打开(字面上)所有的GCC的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用 - GCC具有的所有警告。 (你会认为很容易...)

I would like to enable -- literally -- ALL of the warnings that GCC has. (You'd think it would be easy...)


  • 你会认为 可能会做的伎俩,但不是!仍然需要 -Wextra

  • You'd think -Wall might do the trick, but nope! Still need -Wextra.

你会认为 -Wextra 可能会做的伎俩,但不是!并非列出的所有警告此处(例如, -Wshadow )。

You'd think -Wextra might do the trick, but nope! Not all of the warnings listed here (for example, -Wshadow) are enabled by this. And I still have no idea if this list is comprehensive.

如何告诉GCC启用(无if,

How do I tell GCC to enable (no if's, and's, or but's!) all the warnings it has?

推荐答案

你不能。

GCC 4.4.0手册仅适用于该版本,但它列出了4.4.0的所有可能警告。它们并不都在你链接的页面上,例如一些语言特定的选项在页面上的C + +选项或Obj-C选项。要找到它们,您最好查看选项摘要

The manual for GCC 4.4.0 is only comprehensive for that version, but it does list all the possible warnings for 4.4.0. They're not all on the page you link to though, for instance some language-specific options are on the pages for C++ options or Obj-C options. To find them all you're better off looking at the Options Summary

开启一切会包含 -Wdouble-promotion 与在硬件中实现 float 的32位单精度浮点单元,但在软件中模拟 double 。进行 double 的计算将使用软件仿真并且更慢。这与一些嵌入式CPU相关,但与64位浮点硬件支持的现代桌面CPU完全不相关。

Turning on everything would include -Wdouble-promotion which is only relevant on CPUs with a 32-bit single-precision floating-point unit which implements float in hardware, but emulates double in software. Doing calculations as double would use the software emulation and be slower. That's relevant for some embedded CPUs, but completely irrelevant for modern desktop CPUs with hardware support for 64-bit floating-point.

另一个通常不常用的警告是 -Wtraditional ,它警告在传统C中具有不同含义(或不工作)的完美格式代码,例如stringconcatenation或ISO C函数定义!你真的关心与30岁编译器的兼容性吗?你真的想要一个警告写 int inc(int i){return i + 1; }

Another warning that's not usually useful is -Wtraditional, which warns about perfectly well formed code that has a different meaning (or doesn't work) in traditional C, e.g. "string " "concatenation", or ISO C function definitions! Do you really care about compatibility with 30 year old compilers? Do you really want a warning for writing int inc(int i) { return i+1; } ?

我认为 -Weffc ++ 太嘈杂,关于 Effective C ++ 的过时的第一版,并且提示有关完全有效的C ++的结构(并且在本书的后续版本中更改了指南)。我不想被警告,我haven在我的构造函数中初始化了一个 std :: string 成员;它有一个默认的构造函数,它正是我想要的,为什么我应该写 m_str()来调用它? -Weffc ++ 警告对于编译器来说太难以准确检测(给出假否定),以及那些不有用的警告,例如初始化所有成员

I think -Weffc++ is too noisy to be useful, it's based on the outdated first edition of Effective C++ and warns about constructs which are perfectly valid C++ (and for which the guidelines changed in later editions of the book.) I don't want to be warned that I haven't initialized a std::string member in my constructor; it has a default constructor that does exactly what I want, why should I write m_str() to call it? The -Weffc++ warnings that would be helpful are too difficult for the compiler to detect accurately (giving false negatives), and the ones that aren't useful, such as initializing all members explicitly, just produce too much noise, giving false positives.

Luc Danton提供了一个很好的例子 -Waggregate-return 无用的警告,几乎肯定没有对C ++代码有意义。

Luc Danton provided a great example of useless warnings from -Waggregate-return that almost certainly never makes sense for C++ code.

ie

仔细阅读手册,阅读手册,决定您可以想要启用,试试看吧。阅读你的编译器手册是一个Good Thing TM 无论如何,采取一个简短和启用警告,你不明白是不是一个很好的主意,特别是如果它是为了避免RTFM。

Go through the manual, read about them, decide which you might want to enable, try them. Reading your compiler's manual is a Good ThingTM anyway, taking a short cut and enabling warnings you don't understand is not a very good idea, especially if it's to avoid having to RTFM.

任何刚刚开启的人可能是这样做的,因为他们是无知的,或者一个尖尖的老板说没有警告。

Anyone who just turns on everything is probably either doing so because they're clueless because or a pointy-haired boss said "no warnings."

一些警告很重要,有些警告不重要。你必须是歧视或你搞砸你的程序。例如,考虑 -Wdouble-promotion 。如果你在一个嵌入式系统上工作,你可能需要这个;如果你在桌面系统上工作,你可能不会。你想要 -Wtraditional 吗?

Some warnings are important, and some aren't. You have to be discriminating or you mess up your program. Consider, for instance, -Wdouble-promotion. If you're working on an embedded system you might want this; if you're working on a desktop system you probably don't. And do you want -Wtraditional? I doubt it.

编辑:另请参阅 - Wall-启​​用所有警告,以WONTFIX关闭。

See also -Wall-all to enable all warnings which is closed as WONTFIX.

编辑2: / strong>响应DevSolar的抱怨关于makefiles需要使用不同的警告,根据编译器版本,如果 -Wall -Wextra 不合适,那么使用编译器 - 特定和版本特定的CFLAGS:

Edit 2: in response to DevSolar's complaint about makefiles needing to use different warnings depending on compiler version, if -Wall -Wextra isn't suitable then it's not difficult to use compiler-specific and version-specific CFLAGS:

compiler_name := $(notdir $(CC))
ifeq ($(compiler_name),gcc)
compiler_version := $(basename $(shell $(CC) -dumpversion))
endif
ifeq ($(compile_name),clang)
compiler_version := $(shell $(CC) --version | awk 'NR==1{print $$3}')
endif
# ...
wflags.gcc.base := -Wall -Wextra
wflags.gcc.4.7 := -Wzero-as-null-pointer-constant
wflags.gcc.4.8 := $(wflags.gcc.4.7)
wflags.clang.base := -Wall -Wextra
wflags.clang.3.2 := -Weverything
CFLAGS += $(wflags.$(compiler_name).base) $(wflags.$(compiler_name).$(compiler_version))

这篇关于如何打开(字面上)所有的GCC的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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