我想启用 -- 字面意思 -- 所有 GCC 的警告.(你会认为这很容易...)
I would like to enable -- literally -- ALL of the warnings that GCC has. (You'd think it would be easy...)
您可能会认为 -Wall
可以解决问题,但事实并非如此!仍然需要-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、and 或 but !)所有警告?
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 选项的页面上.要找到它们,您最好查看 Options总结
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位单精度浮点单元的CPU相关
在硬件中,但在软件中模拟 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 中具有不同的含义(或不起作用),例如"string" "concatenation"
,或者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++ 构造(以及指南在本书的后续版本中发生了变化.)我不想被警告说我没有在我的构造函数中初始化一个 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- 的无用警告返回
几乎肯定对 C++ 代码毫无意义.
Luc Danton provided a great example of useless warnings from -Waggregate-return
that almost certainly never makes sense for C++ code.
即你并不真的想要所有警告,你只是认为你想要.
i.e. you don't really want all warnings, you just think you do.
阅读手册,阅读它们,决定您可能想要启用哪些,尝试它们.无论如何,阅读编译器手册是一件好事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-all启用作为 WONTFIX 关闭的所有警告.
Edit 2: 针对 DevSolar 关于 makefile 需要根据编译器版本使用不同警告的抱怨,如果 -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 的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!