推荐的C的gcc警告选项

时间:2020-03-06 14:56:35  来源:igfitidea点击:

除了-Wall,人们还发现了哪些其他警告有用?

http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Warning-Options.html

解决方案

-学徒错误

我通常使用" -W -Wall -ansi -pedantic"进行编译,这有助于确保代码的最高质量和可移植性。

我喜欢-Werror。保持代码警告自由。

我还使用:

-Wstrict-overflow=5

如果我编写依赖于整数的溢出行为的代码,则可能会捕获那些讨厌的bug。

和:

-Wextra

这也使一些不错的选择成为可能。不过,大多数都用于C ++。

我通常使用:

gcc -m64 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual \
        -Wstrict-prototypes -Wmissing-prototypes

对于那些没有使用它的人(我的代码第一次被这些标志编译的人)来说,这个集合吸引了很多人;它很少给我带来问题(尽管-Wcast-qual有时会很麻烦)。

-pedantic -Wall -Wextra -Wno写字符串-Wno未使用参数

对于"伤害我很多"模式,我不使用-Wno ...

我喜欢免费提供我的代码警告,尤其是使用C ++时。尽管通常可以忽略C编译器警告,但是许多C ++警告显示了源代码中的基本缺陷。

-Wfloat-equal,-Wshadow,-Wmissing-prototypes,

-Wredundant-Decls -Wnested-externs -Wstrict原型-Wextra -Werror隐式函数声明-Wunused -Wno-unused值-Wreturn型

我从C ++开始,所以当我转而学习C时,请确保要肛交:

-fmessage-length=0
-ansi -pedantic -std=c99
-Werror
-Wall
-Wextra
-Wwrite-strings
-Winit-self
-Wcast-align
-Wcast-qual
-Wpointer-arith
-Wstrict-aliasing
-Wformat=2
-Wmissing-declarations
-Wmissing-include-dirs
-Wno-unused-parameter
-Wuninitialized
-Wold-style-definition
-Wstrict-prototypes
-Wmissing-prototypes

现在,我使用:

-Wall -W -Wextra -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -Werror

我主要从" gcc入门"一书中获得该列表,然后从Ulrich Drepper关于防御性编程的推荐书中获得该列表(http://people.redhat.com/drepper/Defensive-slides.pdf)。

但是我的名单上没有任何科学依据,感觉就像是一份不错的清单。

/约翰

注意:我不喜欢那些书呆子的标志。

注意:我认为-W和-Wextra或者多或者少是同一件事。

-Wfatal-errors

我一般只是用

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

除非我们指定-O,否则有关未初始化变量的警告将不起作用,因此我将其包括在列表中:

-g -O -Wall -Werror -Wextra -pedantic -std=c99

获取我们所使用的GCC版本的手册,找到所有可用的警告选项,然后仅停用我们有充分理由这样做的警告选项。 (例如,不可修改的第三方标头会给我们很多警告。)记录这些原因。 (在Makefile或者我们设置这些选项的任何位置。)请定期检查设置,并在每次升级编译器时进行检查。

编译器是朋友。警告是朋友。给编译器尽可能多的机会来告诉我们潜在的问题。

截至2011-09-01,使用gcc版本4.6.1

我当前的"开发"别名

gcc -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 -flto -lm

截至2009-11-03

"开发"别名

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 -O0 -ggdb3 -pg --coverage

"发布"别名

gcc -lm -std=c89 -pedantic -O3 -DNDEBUG --combine -fwhole-program -funroll-loops