推荐的海湾合作委员会警告选项

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

请求或取消警告的选项

35667 次浏览

It would be the option -pedantic-errors.

I usually compile with "-W -Wall -ansi -pedantic".

This helps ensure maximum quality and portability of the code.

I like -Werror. It keeps the code warning free.

I also use:

-Wstrict-overflow=5

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

And:

-Wextra

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

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).

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

For "Hurt me plenty" mode, I leave away the -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.

Right now I use:

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

I took that list mostly from the book "An Introduction to GCC" (by rms) and then some from Ulrich Drepper's recommendation about defensive programming (slides for Defensive Programming).

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


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

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

I use this option:

-Wfatal-errors

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

With references:

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

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

Documentation on each warning:

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.

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

As of 2011-09-01, with GCC version 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 -lm

As of 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