5

Possible Duplicate:
Best compiler warning level for C/C++ compilers?

GCC has thousands of options to add more warnings; I was hoping that -Wall -Wextra -pedantic included all the useful ones, but just now I met -Woverloaded-virtual which seems really nice to me.

What other G++ parameters do you use or would you recommend?

Community
  • 1
  • 1
peoro
  • 25,562
  • 20
  • 98
  • 150

5 Answers5

2

Not quite the same category but I always compile with -Werror to flag warnings as errors. Very useful.

To make this work with 3rd party headers, I include those headers via -isystem instead of -I … otherwise warnings in those headers will break the build.

There’s also -Weffc++ which warns for specific issues outlined in Meyers’ Effective C++. However, I’ve found this too harsh. For example, it warns for base classes that don’t declare virtual destructors. In theory, this is very nice but I’m working on a template library that uses inheritance for code reuse (and policy classes) and obviously they don’t have (nor need) virtual destructors.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I agree that `-Weffc++` is too harsh. Once in a while, though, I compile with it to check my code. – maxelost Jan 12 '11 at 09:40
  • 1
    Ironically, `-Weffc++` warns about deriving from the policy class `boost::noncopyable`, but iheriting from such a class is recommended by Effective C++. Similarly, it warns about inheriting `std::unary_function` and friends. GCC should be a bit more smart here: a class without data members and without public member functions is unlikely to be used polymorphically. – Philipp Jan 12 '11 at 10:13
  • 1
    @Philipp: The issues are known and there are several filed bugs and discussions on the GCC mailing list about that … I don’t see this being fixed soon, though. – Konrad Rudolph Jan 12 '11 at 10:24
1

Some that I've seen that are used;

-Wcast-qual: Warn whenever a pointer is cast so as to remove a type qualifier from the target type. For example, warn if a const char * is cast to an ordinary char *.

-Wpointer-arith: Warn about anything that depends on the size of a function type or of void. GNU C assigns these types a size of 1, for convenience in calculations with void * pointers and pointers to functions.

-Wwrite-strings: When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning; when compiling C++, warn about the deprecated conversion from string literals to char *. This warning, by default, is enabled for C++ programs. These warnings will help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes. Otherwise, it will just be a nuisance; this is why we did not make -Wall request these warnings.

-Wdisabled-optimization: Warn if a requested optimization pass is disabled. This warning does not generally indicate that there is anything wrong with your code; it merely indicates that GCC's optimizers were unable to handle the code effectively. Often, the problem is that your code is too big or too complex; GCC will refuse to optimize programs when the optimization itself is likely to take inordinate amounts of time.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
ismail
  • 46,010
  • 9
  • 86
  • 95
1

See Best compiler warning level for C/C++ compilers?. One post contains the following exhaustive (and exhausting) list.

   -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
Community
  • 1
  • 1
EmeryBerger
  • 3,897
  • 18
  • 29
  • Some of those are redundant. For example, the second `-Wall` and the `-Wcomment` are both implied by the first `-Wall`. `-Werror` + `-pedantic` implies `-pedantic-errors`. – Steve Jessop Jan 11 '11 at 22:42
  • 1
    Indeed. In any case, it's overkill. But it's a good starting point. – EmeryBerger Jan 12 '11 at 00:34
1

In general I enable all warnings and then remove those flags selectively that give useless outputs. In one of my projects, I use the following C and C++ warnings:

-pedantic
-Wall
-Wextra
-Wformat=2
-Wmissing-include-dirs
-Wswitch-default
-Wswitch-enum
-Wunused
-Wstrict-aliasing=1
-Wfloat-equal
-Wundef
-Wunsafe-loop-optimizations
-Wpointer-arith
-Wcast-qual
-Wcast-align
-Wwrite-strings
-Wconversion
-Wmissing-format-attribute
-Wpacked
-Wredundant-decls
-Winvalid-pch
-Wvolatile-register-var
-Wsync-nand
-Wsign-conversion
-Wlogical-op
-Wmissing-declarations
-Wmissing-noreturn
-Wstrict-overflow=5
-Wstack-protector

In addition, I use the following C++ flags:

-std=c++98
-Wnon-virtual-dtor
-Wctor-dtor-privacy
-Wstrict-null-sentinel
-Woverloaded-virtual
-Wsign-promo

In addition, for the release build I enable the following warnings:

-pedantic-errors
-Werror
-Wuninitialized
-Winit-self
-Wdisabled-optimization

I find it quite annoying that -Wall enables only the absolute minimum of warnings instead of "all", as the name implies.

Philipp
  • 48,066
  • 12
  • 84
  • 109
  • I would have thought that `-Wdisabled-optimization -Werror` is a bit brave in release mode. Reject any code that's not suitable for *all* optimizations. Or is it a trick to enforce small functions? ;-) – Steve Jessop Jan 11 '11 at 22:47
  • @Steve Jessop: For that project (which is very small) it works, but for other projects I'd disable that flag if it caused warnings. Also, `-Wswitch-enum` can become quite annoying for large enums. – Philipp Jan 12 '11 at 10:09
1

In addition to the ones already mentioned above:

-pedantic                   Issue warnings needed for strict compliance to the standard
-Wall
-Wextra                     Print extra (possibly unwanted) warnings
-Werror                     Treat all warnings as errors
-std=c++0x                  Conform to the ISO 1998 C++ standard, with extensions that are likely to be in C++0x 
-std=c++98                  Conform to the ISO 1998 C++ standard  
Martin York
  • 257,169
  • 86
  • 333
  • 562