我想启用——从字面上看——GCC 的 所有 警告。 (你会认为这很容易……)
我如何告诉 GCC 启用(没有 if’s、and’s 或 but’s!)它有的 所有 警告?
原文由 user541686 发布,翻译遵循 CC BY-SA 4.0 许可协议
我想启用——从字面上看——GCC 的 所有 警告。 (你会认为这很容易……)
我如何告诉 GCC 启用(没有 if’s、and’s 或 but’s!)它有的 所有 警告?
原文由 user541686 发布,翻译遵循 CC BY-SA 4.0 许可协议
我同意前面的答案,即启用所有警告可能没有好处,但是 GCC 确实提供了一种相当方便的方法来实现这一点。命令
gcc -Q --help=warning
提供所有受支持的警告选项的列表以及有关它们是否处于活动状态的信息。顺便说一句,这可以用来找出哪些选项被(不)启用,例如 -Wall
和 -Wextra
gcc -Wall -Wextra -Q --help=warning
要启用所有警告,您可以使用一些正则表达式来提取命令行参数
gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n'
对于我目前的 GCC,这给出了:
-Wabi -Wabi-tag -Waddress -Waggregate-return -Waggressive-loop-optimizations -Waliasing -Walign-commons -Wampersand -Warray-bounds -Warray-temporaries -Wassign-intercept -Wattributes -Wbad-function-cast -Wbool-compare -Wbuiltin-macro-redefined -Wc++-compat -Wc++0x-compat -Wc++14-compat -Wc-binding-type -Wc90-c99-compat -Wc99-c11-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wcharacter-truncation -Wchkp -Wclobbered -Wcomment -Wcompare-reals -Wconditionally-supported -Wconversion -Wconversion-extra -Wconversion-null -Wcoverage-mismatch -Wcpp -Wctor-dtor-privacy -Wdate-time -Wdeclaration -after-statement -Wdelete-incomplete -Wdelete-non-virtual-dtor -Wdeprecated -Wdeprecated-declarations -Wdesignated-init -Wdisabled-optimization -Wdiscarded-array-qualifiers -Wdiscarded-qualifiers -Wdiv-by-zero -Wdouble-promotion -Weffc++ -Wempty-body -Wendif-labels -Wenum-compare -Wextra -Wfloat-equal -Wformat-contains-nul -Wformat-extra-args -Wformat-nonliteral -Wformat-security -Wformat-signedness - Wformat-y2k -Wformat-zero-length -Wfree-nonheap-object -Wfunction-elimination -Wignored-qualifiers -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wimplicit-interface -Wimplicit-procedure -Wincompatible-pointer-types - Winherited-variadic-ctor -Winit-self -Winline -Wint-conversion -Wint-to-pointer-cast -Wintrinsic-shadow -Wintrinsics-std -Winvalid-memory-model -Winvalid-offsetof -Winvalid-pch -Wjump-misses- init -Wline-truncation -Wliteral-suffix -Wlogical-not-parentheses -Wlogical-op -Wlong-long -Wmain -Wmaybe-uninitialized -Wmemset-transposed-args -Wmissing-braces -Wmissing-declarations -Wmissing-field-initializers - Wmissing-include-dirs -Wmissing-parameter-type -Wmissing-prototypes -Wmultichar -Wnarrowing -Wnested-externs -Wnoexcept -Wnon-template-friend -Wnon-virtual-dtor -Wnonnull -Wodr -Wold-style-cast -Wold-样式声明 -Wold 样式定义 -Wopenmp-simd -Woverflow -Woverlength-strings -Woverloaded-virtual -Woverride-init -Wpacked -Wpacked-bitfield-compat -Wpadde d -W括号 -Wpedantic -Wpmf-conversions -Wpointer-arith -Wpointer-sign -Wpointer-to-int-cast -Wpragmas -Wproperty-assign-default -Wprotocol -Wreal-q-constant -Wrealloc-lhs -Wrealloc-lhs-所有 -Wredundant-decls -Wreorder -Wreturn-local-addr -Wreturn-type -Wselector -Wsequence-point -Wshadow -Wshadow-ivar -Wshift-count-negative -Wshift-count-overflow -Wsign-compare -Wsign-promo - Wsized-deallocation -Wsizeof-array-argument -Wsizeof-pointer-memaccess -Wstack-protector -Wstrict-null-sentinel -Wstrict-prototypes -Wstrict-selector-match -Wsuggest-attribute=const -Wsuggest-attribute=format -Wsuggest-属性=noreturn -Wsuggest-attribute=pure -Wsuggest-final-methods -Wsuggest-final-types -Wsuggest-override -Wsurprising -Wswitch -Wswitch-bool -Wswitch-default -Wswitch-enum -Wsync-nand -Wsynth -Wsystem-标题 -Wtabs -Wtarget-lifetime -Wtraditional -Wtraditional-conversion -Wtrampolines -Wtrigraphs -Wtype-limits -Wundeclared-selector -Wundef -Wunderflow -Wuninitialized -Wunknown-pragmas -Wuns afe 循环优化 -Wunsuffixed-float-constants -Wunused -Wunused-but-set-parameter -Wunused-but-set-variable -Wunused-dummy-argument -Wunused-function -Wunused-label -Wunused-local-typedefs - Wunused-macros -Wunused-parameter -Wunused-result -Wunused-value -Wunused-variable -Wuse-without-only -Wuseless-cast -Wvarargs -Wvariadic-macros -Wvector-operation-performance -Wvirtual-move-assign -Wvla - Wvolatile-register-var -Wwrite-strings -Wzero-as-null-pointer-constant -Wzerotrip -frequire-return-statement
这现在可以用来调用 GCC,即
gcc $(gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n')
但是请注意,由于某些警告选项仅适用于某些语言(例如 C++
),因此会导致警告。这些可以通过使用更多正则表达式以仅包含当前语言允许的选项或通过在调用结束时添加适当的 -Wno-whatever
来避免。
原文由 Haatschii 发布,翻译遵循 CC BY-SA 4.0 许可协议
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
3 回答3.4k 阅读
1 回答1.6k 阅读✓ 已解决
你不能。
GCC 4.4.0 的手册仅针对该版本是全面的,但它确实列出了 4.4.0 的所有可能警告。它们并不都在您链接到的页面上。例如,一些特定于语言的选项位于 C++ 选项或 Objective-C 选项的页面上。要找到它们,最好查看 选项摘要
打开 所有内容 将包括
-Wdouble-promotion
这仅与具有 32 位单精度浮点单元的 CPU 相关,该单元在硬件中实现float
但模拟double
在软件中。以double
进行计算将使用软件仿真并且速度较慢。这与某些嵌入式 CPU 相关,但与硬件支持 64 位浮点的现代台式机 CPU 完全无关。另一个通常没有用的警告是
-Wtraditional
,它警告在传统 C 中具有不同含义(或不起作用)的完美格式代码,例如"string " "concatenation"
或 ISO C函数定义!你真的关心与 30 年前的编译器的兼容性吗?你真的想要写int inc(int i) { return i+1; }
的警告吗?我认为
-Weffc++
太吵了,没用。它基于已过时的第一版 _Effective C++_ ,并对完全有效的 C++ 结构提出警告(并且在本书的后续版本中,指南对此进行了更改)。我不想被警告我没有在构造函数中初始化std::string
成员;它有一个默认构造函数,它完全符合我的要求。为什么要写m_str()
来调用它?-Weffc++
有用的警告对于编译器来说太难准确检测(给出假阴性),而那些无用的警告,例如显式初始化所有成员,只会产生太多噪音,给出误报。Luc Danton 提供了一个 很好的例子,说明来自
-Waggregate-return
的无用警告几乎肯定对 C++ 代码毫无意义。即,您并不真正想要 所有 警告;你只是认为你做到了。
通读手册,阅读它们,确定您可能想要启用的内容,然后尝试它们。无论如何,阅读编译器的手册是一件好事,走捷径并启用您不理解的警告并不是一个好主意,尤其是在避免使用 RTFM 时。
任何只是打开 所有东西 的人都可能这样做是因为他们无能为力,或者是一个尖头发的老板说“没有警告”。
有些警告很重要,有些则不重要。你必须有区别,否则你会搞砸你的程序。例如,考虑
-Wdouble-promotion
。如果您正在使用嵌入式系统,您可能需要这个;如果你在桌面系统上工作,你可能不会。你想要-Wtraditional
吗?我对此表示怀疑。另请参阅 -Wall-all 以启用所有 作为 WONTFIX 关闭的警告。
针对 DevSolar 关于 makefile 需要根据编译器版本使用不同警告的抱怨,如果
-Wall -Wextra
不合适,那么使用特定于编译器和特定于版本的 CFLAGS 并不难: