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?
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.This is a set of extra-paranoid flags I'm using for C++ code:
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.
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
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.
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).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).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.