views:

2648

answers:

15

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

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

+1  A: 

-pedantic-errors

Tom Ritter
+1 This will turn compiling into fun. ;)
unexist
+2  A: 

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

Evan Teran
just a note -ansi overrides -std=c99
Sard
Isn't -ansi equivalent to using -std=c89?
Helper Method
+6  A: 

I like -Werror. Keeps the code warning free.

JesperE
Without -Werror all other warning options are pointless. Treating warnings as errors is pretty much the only way to ensure warnings get resolved. If they're just warnings a developer may decide to leave one in because he's sure it's invalid. It may even be true, but the next developer won't fix the warnings he introduced because he didn't see it between all the others, or because it's just one more warning.
Kristof Provost
Your absolutely correct.
JesperE
I disagree with Kristof, because many times, I'd rather just get a working copy compiled first and _then_ address the errors.
Yktula
I understand why that is tempting, but when/if you have a working copy you will be more likely to leave it as it is because "it works". This risk is even higher in a corporate environment, where you'll have to convince your boss to leave you some time to fix the warnings.
JesperE
+4  A: 

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.

Nils Pipenbrinck
-Wextra seems to be the new name for -W (Which is also still supported)
Sard
+11  A: 

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

Jonathan Leffler
These days, I find I have to add '`-Wdeclaration-after-statement`' in order to detect code that MSVC (which is still basically a C89 compiler) won't handle. 'Tis a nuisance. Adding '`-Wextra`' can spot some other problems too.
Jonathan Leffler
+1  A: 

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

Thorsten79
Why -Wno-write-strings?
Tom
Because the toolchain is at liberty to put string literals into read-only memory.
DevSolar
+1  A: 

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

Mark Bessey
+1  A: 

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

florin
+2  A: 

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

-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
Tom
Can you use -ansi -pedantic -std=c99 at the same time? Is not -ansi approximately the same thing as c89? and if so how does that work with the c99 flag?
Johan
@Johan - you can, and it's not actually necessary, as I've found out more recently. -ansi implies -std=<default>, so really you could just say -std=c99 -pedantic and get exactly the same effect. I do tend to use it anyways, just for the documentation effect. I feel that it reads, "This code is ANSI-standard (pedantic!), using standard C99." Immediately afterwards usually comes -Wno-long-long or similar... any exceptions to the ANSI standard.
Tom
+1  A: 

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" and then some from Ulrich Drepper recomendation about Defensive Programming (http://people.redhat.com/drepper/Defensive-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.

Johan
Afer using -Wconversion, and spending a couple of hours testing various data types in my code and re-building, I researched -Wconversion and would not recommend using it in general. The problem being it generates warnings about code such as: char a = 5; char b = a - 1; This is using gcc 4.3.2 (Debian 4.3.2.-1.1)
James Morris
-Wconversion warnings can be eliminated by (for example in above comment): char a = 5; char b = (char)(a - 1); note brackets.
James Morris
A: 

-Wfatal-errors

Adrian Panasiuk
A: 

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
amaterasu
A: 

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
jleedev
+3  A: 

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.

DevSolar
+1  A: 

My current "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

And the "release" alias

gcc -lm -std=c89 -pedantic -O3 -DNDEBUG --combine -fwhole-program -funroll-loops
pmg
`-Wfloat-equal` added to my alias. Thank you Mark
pmg