Best compiler warning level for C/C++ compilers? [closed] Best compiler warning level for C/C++ compilers? [closed] c c

Best compiler warning level for C/C++ compilers? [closed]


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.


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