除了-Wall,还有哪些警告被人们认为是有用的?
我经常使用:
gcc -m64 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual \
-Wstrict-prototypes -Wmissing-prototypes
这个设置对于不习惯它的人(第一次使用这些标志编译代码的人)很容易出现问题,但很少给我带来麻烦(尽管 -Wcast-qual 偶尔会有些麻烦)。
-m32
和-m64
(当然是在不同的运行中)可以更好地保护您免受在使用printf()
和scaf()
转换规范时出现的各种错误的影响。 - Jonathan Lefflergcc -std=c89 -pedantic -Wall \
-Wno-missing-braces -Wextra -Wno-missing-field-initializers \
-Wformat=2 -Wswitch-default -Wswitch-enum -Wcast-align \
-Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \
-Wstrict-prototypes -Winline -Wundef -Wnested-externs \
-Wcast-qual -Wshadow -Wunreachable-code -Wlogical-op \
-Wfloat-equal -Wstrict-aliasing=2 -Wredundant-decls \
-Wold-style-definition -Werror \
-ggdb3 \
-O0 \
-fno-omit-frame-pointer -ffloat-store \
-fno-common -fstrict-aliasing \
-lm
gcc -std=c89 -pedantic -O3 -DNDEBUG -lm
截至2009年11月3日,“开发”别名
gcc -Wall -Wextra -Wformat=2 -Wswitch-default -Wcast-align \
-Wpointer-arith -Wbad-function-cast -Wstrict-prototypes \
-Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow \
-Wwrite-strings -Wconversion -Wunreachable-code \
-Wstrict-aliasing=2 \
-ffloat-store -fno-common -fstrict-aliasing \
-lm -std=c89 -pedantic -O3 -DNDEBUG -pg --coverage
gcc -lm -std=c89 -pedantic -O3 -DNDEBUG --combine \
-fwhole-program -funroll-loops
-Wstrict-aliasing=2
实际上会降低由-Wall
隐含的-Wstrict-aliasing=3
警告级别。 - nwellnhof-combine
was removed in GCC 4.6.0 (released 2011-03-25): "The C-only intermodule optimization framework (IMA, enabled by -combine) has been removed in favor of the new generic link-time optimization framework (LTO) introduced in GCC 4.5.0.". The last version was 4.5.4. It is listed as -combine
, not --combine
- Peter Mortensen我最开始学习的是C++,所以当我转而学习C语言时,我确保自己更加谨慎:
-pedantic-errors
吗?它和-Werror=pedantic
不一样。 - x-yuri-Wstrict-aliasing
现在已经成为-Wall
的一部分。 - yugr我喜欢使用-Werror选项。它可以保持代码无警告。
#warning
是使用-Werror
的一个好的副作用。 - JesperE-Werror
进行构建,这会令使用不同编译器版本的打包者感到困扰,因为警告有时会随着编译器版本的变化而改变,有些对你而言无警告的代码可能会对其他人产生警告,然后他们就必须深入你的构建系统来关闭它。 - Spudd86获取您使用的GCC版本的手册,找到所有可用的警告选项,然后仅停用那些您有强烈理由停用的选项。(例如,无法修改的第三方头文件,在其他情况下会给您带来大量警告。)记录下这些理由。(在Makefile或设置这些选项的任何地方。)定期检查设置,以及每次升级编译器时。
编译器是您的朋友,警告也是您的朋友。尽可能让编译器向您报告可能存在的问题。
-pedantic、-Wall、-Wextra、-Wno-write-strings、-Wno-unused-parameter
对于“Hurt me plenty”模式,我会省略 -Wno...。
我喜欢我的代码没有警告,特别是在 C++ 中。虽然 C 编译器警告经常可以忽略,但许多 C++ 警告显示源代码中的基本缺陷。
Foo(int dndu, int dndv):dndu_(dndu),dndv_(dndu){}
触发->相对难以发现。如果你对这个警告感到烦恼,你应该将参数foo (int /*q*/)
注释掉,这也提高了你代码的可读性。 - Sebastian Mach目前我使用:
-Wall -W -Wextra -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -Werror
我大部分是从书籍《An Introduction to GCC》(作者:rms)中获取的,然后也参考了Ulrich Drepper关于防御式编程的建议(幻灯片链接:Defensive Programming)。
但是我并没有科学的依据来选择这个列表,只是感觉这个列表不错。
注意:我不喜欢那些过于严格的标志...
注意:我认为-W和-Wextra基本相同。
我通常只使用
gcc -Wall -W -Wunused-parameter -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wsign-compare -Wconversion -Wshadow -Wcast-align -Wparentheses -Wsequence-point -Wdeclaration-after-statement -Wundef -Wpointer-arith -Wnested-externs -Wredundant-decls -Werror -Wdisabled-optimization -pedantic -funit-at-a-time -o
参考资料: