views:

2807

answers:

12

What compiler warning level do you recommend for different C/C++ compilers?

gcc and g++ will let you get away with a lot on the default level. I find the best warning level for me is '-Wall'. And I always try to remove fix the code for the warnings it generates. (Even the silly ones about using parenthesis for logical precedence rules or to say I really mean 'if (x = y)')

What are your favorite levels for the different compilers, such as Sun CC, aCC (HPUX ?), Visual Studio, intel?

Edit:

I just wanted to point out that I don't use "-Werror" (but I do understand it's utility) on gcc/g++ because, I use:

#warning "this is a note to myself"

in a few places in my code. Do all the compilers understand the #warning macro?

+4  A: 

I tend to use -Wall (because everyone does make bugs, nobody is perfect) , but i don't use -Werror (treat warnings as errors) because now and then gcc warns about things which are right anyway (false positives).

Johannes Schaub - litb
So, if gcc throws 100 warnings at you, do you review them all every time you compile? Otherwise, I don't see how you're dealing with the real warnings buried in the pile of the false-positives...
Paulius Maruška
I don't use Werror either. There are warnings (unused functions) coming from flex/bison generated source code that would be difficult to suppress.
You can work around the flex/bison warnings by using the techniques described here: http://wiki.services.openoffice.org/wiki/Writing_warning-free_code
codelogic
Paulius, I find it unlikely that every time you compile that you will get fresh sets of 100s of warnings, assuming of course that code is compiled fairly frequently. I can see it happening the first time though.
codelogic
Paulius, if it throws 100 warnings at me, i'm going to fix those so i only have a few warnings left that are i don't have to worry about. using good libraries make it possible not having pages of warning messages. also, what codelogic says.i have regular "warning killing" phases where i kill them :)
Johannes Schaub - litb
codelogic, thanks for the link. It turns out there is only one warning I have left in flex. I use:%option noyywrap%option nounputto suppress warnings about these functions. The one I can't get rid of:Scanner.cc:1241: warning: comparison between signed and unsigned integer expressions
I use `-W` (aka `-Wextra`) in addition to `-Wall` as it catches some extra warnings mainly due to typos and forgotten code paths (oops, forgot to return something!). It's really helpful in catching silly bugs at the compile stage.
strager
litb, so what about the warnings that you "don't have to worry about"? do you just leave them? I would assume that the compiler would show those warnings every time it compiles. Over time, the number of such warnings would, probably, increase... How do you find the real warnings then?
Paulius Maruška
Continued: And if you're not leaving those warnings, then I don't see any reason not use -Werror, because you're fixing them all anyway...
Paulius Maruška
Paulius, it happens quite seldomly that i get a warning that i won't fix. but when that's the case, then will indeed just leave it. emacs is fine in coloring the warnings, so i won't miss others :)
Johannes Schaub - litb
anyway, i feel it's better to enable all warnings, than enable only a few but enable -Werror just because otherwise one wouldn't be able to compile without errors. but this seems to be a quite subjective matter :)
Johannes Schaub - litb
When teaching C a long time ago, the supplied library headers caused warnings. Sigh.
David Thornley
David, gcc has special ways to deal with that: http://gcc.gnu.org/onlinedocs/cpp/System-Headers.html
Johannes Schaub - litb
+11  A: 

On Visual C++, I use /W4 and /WX (treat warnings as errors).

VC also has /Wall, but it's incompatible with the standard headers.

I choose to treat warnings as errors, because that forces me to fix them. I fix all warnings, even if that means adding #pragma to ignore the warning - that way, I'm stating explicitly, that I'm aware of the warning (so other developers won't e-mail me about it).

Paulius Maruška
We have specific warnings we explicitly don't pay attention to, and a clean compile (no warnings) policy on everything else.
David Thornley
+6  A: 

I agree with litb to always use -Wall. In addition, if you want to ensure your code is compliant you can also use -pedantic. Another warning that can be helpful if you're handling unions and structs at the byte level is -Wpadded.

codelogic
Yay for -pedantic
gnud
+1  A: 

In Visual C I use /w3. I find w4 throws up too much noise (lots of it from the MS libraries) to go through on every build. The extra warnings are very minor and haven't been a cause of a bug so far.

Patrick
+7  A: 

I believe VC also supports

#pragma message ("note to self")

But as the system grows and grows, and you get a nightly build 30 developers work on simultaneously, it takes days to read all the notes to self, even in that amount that self is going to be do nothing but note reading and finally going to break under the stress not being able to keep up and have to resign...

No really, the amount of warnings is quickly going to grow if you allow them, and you won't be able to spot the really important ones (uninitialized variables, this pointer used in constructor, ...).

That's why I try to treat warnings as errors: most of the time, the compiler is right warning me, and if he isn't, I document it in the code and prepend

#pragma warning ( push )
#pragma warning ( 4191 : disable )
// violent code, properly documented
#pragma warning ( pop )

I just read they have a warning ( N : suppress ) pragma, too.

xtofl
Thanks for your comments. Right now I am just one developer. And I use the warnings to remind me where I have left off.
+3  A: 

I do all development with Warning as Errors turned on.

Since I still develop in VC6 I have a lot of #pragma's in my code (4786 mainly).

graham.reeds
+2  A: 

I like -Wall and strict prototypes as well as implicit function definitions. Errors on those can be very useful.

On Unix-like systems you have to obey the user's ENV preferences .. so what they see and report might be entirely different than what you need :)

I'm also a type punning fiend, so I tend to set -Fno-strict-aliasing as well, unless the user wants it. Safe memory management in classic C is hard to accomplish otherwise.

Tim Post
+1  A: 

There's a nice list of options for GCC here: http://mces.blogspot.com/2008/12/year-end-cleaning-ie-on-warning-options.htm. -Wall does not enable all the possible warnings, and some have to be enabled explicitely.

Marco
Better yet, see the GCC manual: http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Warning-Options.html
strager
A: 

Thanks everyone for their answers. It has been a while since I've used anything but gcc/g++. Ones I've had to use a long time ago are

-fmessage-length = 0 (since g++ had an ugly habit of line breaking messages)

-Wno-deprecated      (since I worked on a code base pre-existing the std namespace)

I do remember that (at least 5 years ago) anything above the default warning level on the Sun Workshop CC compiler was too much. I also think this may have been true for the Intel compiler. I have not been up to date with non gnu compilers for a while.

+1  A: 

This is a set of extra-paranoid flags I'm using for C++ code:

    -g -O -Wall -Weffc++ -pedantic  \
    -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align \
    -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion \
    -Wdisabled-optimization \
    -Werror -Wfloat-equal  -Wformat  -Wformat=2 \
    -Wformat-nonliteral -Wformat-security  \
    -Wformat-y2k \
    -Wimplicit  -Wimport  -Winit-self  -Winline \
    -Winvalid-pch   \
    -Wunsafe-loop-optimizations  -Wlong-long -Wmissing-braces \
    -Wmissing-field-initializers -Wmissing-format-attribute   \
    -Wmissing-include-dirs -Wmissing-noreturn \
    -Wpacked  -Wpadded -Wparentheses  -Wpointer-arith \
    -Wredundant-decls -Wreturn-type \
    -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector \
    -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default \
    -Wswitch-enum -Wtrigraphs  -Wuninitialized \
    -Wunknown-pragmas  -Wunreachable-code -Wunused \
    -Wunused-function  -Wunused-label  -Wunused-parameter \
    -Wunused-value  -Wunused-variable  -Wvariadic-macros \
    -Wvolatile-register-var  -Wwrite-strings

That should give you something to get started. Depending on a project, you might need to tone it down in order to not see warning coming from third-party libraries (which are usually pretty careless about being warning free.) For example, Boost vector/matrix code will make g++ emit a lot of noise.

A better way to handle such cases is to write a wrapper around g++ that still uses warnings tuned up to max but allows one to suppress them from being seen for specific files/line numbers. I wrote such a tool long time ago and will release it once I have time to clean it up.

Alexander L. Belikoff
It's good to know I am not alone; You use almost as many as I do :)
Justin
A: 

The GCC compilers become stricter with every new version. Use the flag -ansi to produce warnings for violations of the strictest interpretation of the ANSI language standards. That's usually stuff that just happens to work in your current compiler, but may produce errors in the next version or in other compilers. That flag will help you avoid having to port your code every time you switch compilers/versions.

palm3D
gcc -ansi is equivalent to 'gcc -std=c89'; if that's what you want, it is OK, but if you prefer C99, use '-std=c99'.
Jonathan Leffler
A: 

On GCC, for preference I use "-Wall -Wextra -Wwrite-strings -Werror", and also specify a standard with std=. Which standard depends on the project: principally on how portable it needs to be.

The reason I use -Werror is that warnings are unacceptable (to me) even if they don't represent a real bug. I'd rather work around whatever caused the warning, than have to ignore warnings every single time I compile for the rest of my life. Once you allow warnings in the compile, it's just too easy to miss one that wasn't there last time.

Of course when dealing with third-party code, sometimes you can't get rid of the warnings. Then I'd decide on a case-by-case basis whether to relax the -W options, remove -Werror and write a script to check that only expect warnings occur, or maybe modify the third-party code (either to "fix" the warning or to disable it with pragmas if possible).

Steve Jessop