47

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?

1
  • 1
    #warning is non-standard; I think #error is pretty universal, though
    – Mikeage
    Jan 1, 2009 at 13:53

14 Answers 14

51

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

    -g -O -Wall -Weffc++ -pedantic  \
    -pedantic-errors -Wextra -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.

3
  • 1
    Someone asked a similar question as gcc-specific, and this post was referenced. I updated this answer to remove duplicate warnings (for instance, -Wformat=2 implies -Wformat-y2k). See: stackoverflow.com/a/9862800/852254 Mar 28, 2012 at 0:37
  • g++ won't build with -Wimplicit, is C/ObjC specific.
    – user1244033
    Aug 23, 2014 at 22:37
  • 1
    There seems to be much redundancy here. For example (at least on GCC) -Wall implies -Wunused. Also, this answer would be much better is you explained the flags.
    – Lii
    Feb 3, 2016 at 13:04
25

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

1
  • 2
    We have specific warnings we explicitly don't pay attention to, and a clean compile (no warnings) policy on everything else. Dec 30, 2008 at 17:48
14

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.

0
10

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

13
  • 1
    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
    Dec 30, 2008 at 9:34
  • 1
    You can work around the flex/bison warnings by using the techniques described here: wiki.services.openoffice.org/wiki/Writing_warning-free_code
    – codelogic
    Dec 30, 2008 at 9:40
  • 2
    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 :) Dec 30, 2008 at 9:46
  • 1
    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
    Dec 30, 2008 at 11:12
  • 1
    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 :) Dec 30, 2008 at 13:47
8

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.

0
4

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

3

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.

1
3

I like -Wall and strict prototypes as well as implicit function definitions. Errors on those can be very useful. There's also -Wextra which will pick up all kinds of things like things you intended to be conditionals but accidentally wrote as statements:

if (something);
   classic_way_to_leak_memory();

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.

3

no one has mentioned the Intel compiler yet:

https://software.intel.com/sites/products/documentation/doclib/iss/2013/compiler/cpp-lin/GUID-D060680A-1A18-4574-8291-5C74E6E31335.htm

-w3 is pretty chatty, so I would suggest -w2

3

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

1

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.

1

I also like check all possible warnings that give compiler in my project. Unfortunately answer about Intel C++ compiler was not very informative for me (link is dead). I made my own research.

Because i use Qt 5 and qmake i have predefined warning level -w1. Can't do nothing with that. But this is not all and ICC has more keys:

-Wcomment 
-Weffc++ 
-Wextra-tokens 
-Wformat 
-Winline // don't use, show only for example
-Wmain 
-Wmissing-declarations 
-Wmissing-prototypes 
-Wnon-virtual-dtor 
-Wp64 
-Wpointer-arith 
-Wremarks 
-Wreturn-type 
-Wsign-compare 
-Wstrict-aliasing 
-Wstrict-prototypes 
-Wtrigraphs 
-Wuninitialized 
-Wunknown-pragmas 
-Wunused-variable

More about all keys.

Also i want to add that, unlike GCC, ICC produces several warnings for one key, for example key -Weffc++. In case you want only see several warnings from all list use key -wd.

I disable: -wd1418,2012,2015,2017,2022,2013. And warnings -wd1572,873,2259,2261 was disabled in qmake by default.

I use PCH and have found very annoying to see in Qt Creator messages about using PCH file like error. To disable, use -Wno-pch-messages.

For disabling warning in code i use:

#if defined(Q_CC_INTEL)
    #pragma warning( push )
    #pragma warning( disable: 2021 )
#endif

// some code

#if defined(Q_CC_INTEL)
    #pragma warning( pop )
#endif
0

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.

0

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.

1
  • 3
    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'. Dec 30, 2008 at 21:18