Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: What GNU G++ parameters? [duplicate]

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?

like image 362
peoro Avatar asked Jan 11 '11 19:01

peoro


4 Answers

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.

like image 134
Konrad Rudolph Avatar answered Nov 19 '22 04:11

Konrad Rudolph


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
like image 20
EmeryBerger Avatar answered Nov 19 '22 05:11

EmeryBerger


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.

like image 1
Philipp Avatar answered Nov 19 '22 05:11

Philipp


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.

like image 1
ismail Avatar answered Nov 19 '22 06:11

ismail