为C推荐gcc警告选项。

时间:2022-07-01 06:59:40

Other than -Wall what other warnings have people found useful?

除了长城以外,人们还发现了哪些有用的警告?

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

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

15 个解决方案

#1


45  

I routinely use:

我经常使用:

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

This set catches a lot for people unused to it (people whose code I get to compile with those flags for the first time); it seldom gives me a problem (though -Wcast-qual is occasionally a nuisance).

这个集合捕获了很多未使用的人(我第一次使用这些标志来编译代码的人);它很少给我一个问题(尽管-Wcast-qual有时是一个麻烦)。

#2


33  

As of 2011-09-01, with gcc version 4.6.1

到2011-09-01,gcc版本4.6.1。

My current "development" alias

我现在的“发展”别名

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

The "release" alias

“释放”的别名

gcc -std=c89 -pedantic -O3 -DNDEBUG -flto -lm

As of 2009-11-03

截至2009-11-03

"development" alias

“发展”别名

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

"release" alias

“释放”别名

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

#3


15  

I like -Werror. Keeps the code warning free.

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

#4


14  

I started out with C++, so when I made the switch to learning C I made sure to be extra-anal:

我从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

#5


9  

Get the manual for the GCC version you use, find all warning options available, and then deactivate only those for which you have a compelling reason to do so. (For example, non-modifiable third-party headers that would give you lots of warnings otherwise.) Document those reasons. (In the Makefile or wherever you set those options.) Review the settings at regular intervalls, and whenever you upgrade your compiler.

获取您所使用的GCC版本的手册,找到所有可用的警告选项,然后仅禁用那些您有充分理由这样做的选项。(例如,不可修改的第三方报头,否则会给你很多警告。)记录这些原因。(在Makefile中或任何设置这些选项的地方)。检查常规intervalls的设置,以及何时升级编译器。

The compiler is your friend. Warnings are your friend. Give the compiler as much chance to tell you of potential problems as possible.

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

#6


5  

I also use:

我也使用:

-Wstrict-overflow=5

-Wstrict-overflow = 5

To catch those nasty bugs that may occur if I write code that relies on the overflow behaviour of integers.

如果我编写依赖于整数溢出行为的代码,就可以捕捉到这些讨厌的bug。

And:

和:

-Wextra

-Wextra

Which enables some options that are nice to have as well. Most are for C++ though.

这使得一些选项也很好。不过大多数是为c++编写的。

#7


2  

I usually compile with "-W -Wall -ansi -pedantic" this helps ensure maximum quality and portability of the code.

我通常用“-W -Wall -ansi -pedantic”来编译,这有助于确保代码的最大质量和可移植性。

#8


2  

-pedantic -Wall -Wextra -Wno-write-strings -Wno-unused-parameter

-pedantic -Wall -Wextra - wno -write-string - wno - unusedparameter。

For "Hurt me plenty" mode, I leave away the -Wno...

因为“伤害我很多”的模式,我离开了-Wno…

I like to have my code warning free, especially with C++. While C compiler warnings can often be ignored, many C++ warnings show fundamental defects in the source code.

我希望我的代码是免费的,特别是c++。虽然C编译器警告常常被忽略,但是许多c++警告在源代码中显示了基本的缺陷。

#9


1  

-pedantic-errors

-pedantic-errors

#10


1  

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

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

#11


1  

-Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wextra -Werror-implicit-function-declaration -Wunused -Wno-unused-value -Wreturn-type

- wxd - wnested - wnested - wnested - wnested - wnested - wnet - - - wout - wout - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#12


1  

Right now I use:

现在我使用:

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

- ww - wp - wcast -Wcast-qual - wwrite -Werror。

I took that list mostly from the book "An introduction to gcc" and then some from Ulrich Drepper recomendation about Defensive Programming (http://people.redhat.com/drepper/Defensive-slides.pdf).

我主要从《海湾合作委员会的介绍》这本书中获得了这份名单,然后是一些来自Ulrich Drepper recomendation的关于防御编程的文章(http://people.redhat.com/drepper/defensi-slides.pdf)。

But I don't have any science behind my list, it just felt like a good list.

但我的名单上没有任何科学依据,它只是一个很好的清单。

/Johan

/约翰


Note: I don't like those pedantic flags though....

注意:我不喜欢那些迂腐的旗帜虽然....

Note: I think that -W and -Wextra are more or less the same thing.

注意:我认为-W和-Wextra或多或少是相同的。

#13


1  

I generally just use

我通常只是用

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

#14


1  

The warning about uninitialized variables doesn't work unless you specify -O, so I include that in my list:

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

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

#15


0  

-Wfatal-errors

-Wfatal-errors

#1


45  

I routinely use:

我经常使用:

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

This set catches a lot for people unused to it (people whose code I get to compile with those flags for the first time); it seldom gives me a problem (though -Wcast-qual is occasionally a nuisance).

这个集合捕获了很多未使用的人(我第一次使用这些标志来编译代码的人);它很少给我一个问题(尽管-Wcast-qual有时是一个麻烦)。

#2


33  

As of 2011-09-01, with gcc version 4.6.1

到2011-09-01,gcc版本4.6.1。

My current "development" alias

我现在的“发展”别名

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

The "release" alias

“释放”的别名

gcc -std=c89 -pedantic -O3 -DNDEBUG -flto -lm

As of 2009-11-03

截至2009-11-03

"development" alias

“发展”别名

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

"release" alias

“释放”别名

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

#3


15  

I like -Werror. Keeps the code warning free.

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

#4


14  

I started out with C++, so when I made the switch to learning C I made sure to be extra-anal:

我从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

#5


9  

Get the manual for the GCC version you use, find all warning options available, and then deactivate only those for which you have a compelling reason to do so. (For example, non-modifiable third-party headers that would give you lots of warnings otherwise.) Document those reasons. (In the Makefile or wherever you set those options.) Review the settings at regular intervalls, and whenever you upgrade your compiler.

获取您所使用的GCC版本的手册,找到所有可用的警告选项,然后仅禁用那些您有充分理由这样做的选项。(例如,不可修改的第三方报头,否则会给你很多警告。)记录这些原因。(在Makefile中或任何设置这些选项的地方)。检查常规intervalls的设置,以及何时升级编译器。

The compiler is your friend. Warnings are your friend. Give the compiler as much chance to tell you of potential problems as possible.

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

#6


5  

I also use:

我也使用:

-Wstrict-overflow=5

-Wstrict-overflow = 5

To catch those nasty bugs that may occur if I write code that relies on the overflow behaviour of integers.

如果我编写依赖于整数溢出行为的代码,就可以捕捉到这些讨厌的bug。

And:

和:

-Wextra

-Wextra

Which enables some options that are nice to have as well. Most are for C++ though.

这使得一些选项也很好。不过大多数是为c++编写的。

#7


2  

I usually compile with "-W -Wall -ansi -pedantic" this helps ensure maximum quality and portability of the code.

我通常用“-W -Wall -ansi -pedantic”来编译,这有助于确保代码的最大质量和可移植性。

#8


2  

-pedantic -Wall -Wextra -Wno-write-strings -Wno-unused-parameter

-pedantic -Wall -Wextra - wno -write-string - wno - unusedparameter。

For "Hurt me plenty" mode, I leave away the -Wno...

因为“伤害我很多”的模式,我离开了-Wno…

I like to have my code warning free, especially with C++. While C compiler warnings can often be ignored, many C++ warnings show fundamental defects in the source code.

我希望我的代码是免费的,特别是c++。虽然C编译器警告常常被忽略,但是许多c++警告在源代码中显示了基本的缺陷。

#9


1  

-pedantic-errors

-pedantic-errors

#10


1  

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

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

#11


1  

-Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wextra -Werror-implicit-function-declaration -Wunused -Wno-unused-value -Wreturn-type

- wxd - wnested - wnested - wnested - wnested - wnested - wnet - - - wout - wout - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#12


1  

Right now I use:

现在我使用:

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

- ww - wp - wcast -Wcast-qual - wwrite -Werror。

I took that list mostly from the book "An introduction to gcc" and then some from Ulrich Drepper recomendation about Defensive Programming (http://people.redhat.com/drepper/Defensive-slides.pdf).

我主要从《海湾合作委员会的介绍》这本书中获得了这份名单,然后是一些来自Ulrich Drepper recomendation的关于防御编程的文章(http://people.redhat.com/drepper/defensi-slides.pdf)。

But I don't have any science behind my list, it just felt like a good list.

但我的名单上没有任何科学依据,它只是一个很好的清单。

/Johan

/约翰


Note: I don't like those pedantic flags though....

注意:我不喜欢那些迂腐的旗帜虽然....

Note: I think that -W and -Wextra are more or less the same thing.

注意:我认为-W和-Wextra或多或少是相同的。

#13


1  

I generally just use

我通常只是用

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

#14


1  

The warning about uninitialized variables doesn't work unless you specify -O, so I include that in my list:

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

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

#15


0  

-Wfatal-errors

-Wfatal-errors