Flags to enable thorough and verbose g++ warnings
D'oh, all of my original searches turned up 99% of posts on how to suppress warnings (scarily enough), but I just ran across this comment, which has this lovely set of flags (some less relevant):
Cross checked with:
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
-g -O -Wall -Weffc++ -pedantic \
-pedantic-errors -Wextra -Waggregate-return -Wcast-align \
-Wcast-qual -Wconversion \
-Wdisabled-optimization \
-Werror -Wfloat-equal -Wformat=2 \
-Wformat-nonliteral -Wformat-security \
-Wformat-y2k \
-Wimplicit -Wimport -Winit-self -Winline \
-Winvalid-pch \
-Wlong-long \
-Wmissing-field-initializers -Wmissing-format-attribute \
-Wmissing-include-dirs -Wmissing-noreturn \
-Wpacked -Wpadded -Wpointer-arith \
-Wredundant-decls \
-Wshadow -Wstack-protector \
-Wstrict-aliasing=2 -Wswitch-default \
-Wswitch-enum \
-Wunreachable-code -Wunused \
-Wunused-parameter \
-Wvariadic-macros \
-Wwrite-strings
So, I think that's a good starting point. Didn't realize this was a dupe, but at least it was deeply buried. :-)
Better compiler warnings?
The option you are after is likely -Wmaybe-uninitialized
or -Wuninitialized
. Both of these are part of the -Wall
option which turns on these, and many other warnings (the -Wxxx
options are related to warnings).
Full documentation of the warning options for gcc may be read at: Options to Request or Suppress Warnings in the gcc documentation.
You may also find that -Wextra
may be of use to you (either alone, or in conjunction with -Wall
). -Wextra
also enables -Wuninitialized
, but it has other items that are not always set by -Wall
and are ones that I like to see (things like -Wunused-parameter
and -Wunused-but-set-parameter
)... though thats not specific to this bit of code.
That said... (yea, there's a "that said")... I can't seem to tickle an error with this functionality with gcc that is is available on http://gcc.godbolt.org.
Extending your code with some that is specifically described in the documentation for -Wmaybe-uninitialized
#include <iostream>
using namespace std;
int main() {
int a, b;
int x, y, z;
// warning: variable ‘y’ set but not used [-Wunused-but-set-variable]
cin >> a;
while (a>0) b++;
switch(a) {
case 1: x = 1; y = 1; z++; break;
// warning: ‘z’ may be used uninitialized in this function [-Wmaybe-uninitialized]
case 2: x = 4; y = 2; break;
case 3: x = 5; y = 3;
}
cout << a;
cout << b;
cout << x;
}
This is an attempt to tickle a number of the unused and unitized warnings. I was able to get the z
variable to produce the error, but for some reason b++
in that while loop does not generate an error or warning with gcc (tested using gcc 4.9.0).
clang version 3.4.1, however, does produce warnings with these command line options for both b
and z
And while -Wall
and -Wextra
should produce the warnings you are after, for some reason they do not produce the desired warnings for this specific piece of code in gcc 4.9.0
How can I turn on (literally) ALL of GCC's warnings?
You can't.
The manual for GCC 4.4.0 is only comprehensive for that version, but it does list all the possible warnings for 4.4.0. They're not all on the page you link to though. For instance, some language-specific options are on the pages for C++ options or Objective-C options. To find them all, you're better off looking at the Options Summary
Turning on everything would include -Wdouble-promotion
which is only relevant on CPUs with a 32-bit single-precision floating-point unit which implements float
in hardware, but emulates double
in software. Doing calculations as double
would use the software emulation and be slower. That's relevant for some embedded CPUs, but completely irrelevant for modern desktop CPUs with hardware support for 64-bit floating-point.
Another warning that's not usually useful is -Wtraditional
, which warns about perfectly well formed code that has a different meaning (or doesn't work) in traditional C, e.g., "string " "concatenation"
, or ISO C function definitions! Do you really care about compatibility with 30 year old compilers? Do you really want a warning for writing int inc(int i) { return i+1; }
?
I think -Weffc++
is too noisy to be useful. It's based on the outdated first edition of Effective C++ and warns about constructs which are perfectly valid C++ (and for which the guidelines changed in later editions of the book). I don't want to be warned that I haven't initialized a std::string
member in my constructor; it has a default constructor that does exactly what I want. Why should I write m_str()
to call it? The -Weffc++
warnings that would be helpful are too difficult for the compiler to detect accurately (giving false negatives), and the ones that aren't useful, such as initializing all members explicitly, just produce too much noise, giving false positives.
Luc Danton provided a great example of useless warnings from -Waggregate-return
that almost certainly never makes sense for C++ code.
I.e., you don't really want all warnings; you just think you do.
Go through the manual, read about them, decide which you might want to enable, and try them. Reading your compiler's manual is a Good ThingTM anyway, taking a shortcut and enabling warnings you don't understand is not a very good idea, especially if it's to avoid having to RTFM.
Anyone who just turns on everything is probably either doing so because they're clueless because or a pointy-haired boss said "no warnings."
Some warnings are important, and some aren't. You have to be discriminating or you mess up your program. Consider, for instance, -Wdouble-promotion
. If you're working on an embedded system you might want this; if you're working on a desktop system you probably don't. And do you want -Wtraditional
? I doubt it.
See also -Wall-all to enable all warnings which is closed as WONTFIX.
In response to DevSolar's complaint about makefiles needing to use different warnings depending on compiler version, if -Wall -Wextra
isn't suitable then it's not difficult to use compiler-specific and version-specific CFLAGS:
compiler_name := $(notdir $(CC))
ifeq ($(compiler_name),gcc)
compiler_version := $(basename $(shell $(CC) -dumpversion))
endif
ifeq ($(compile_name),clang)
compiler_version := $(shell $(CC) --version | awk 'NR==1{print $$3}')
endif
# ...
wflags.gcc.base := -Wall -Wextra
wflags.gcc.4.7 := -Wzero-as-null-pointer-constant
wflags.gcc.4.8 := $(wflags.gcc.4.7)
wflags.clang.base := -Wall -Wextra
wflags.clang.3.2 := -Weverything
CFLAGS += $(wflags.$(compiler_name).base) $(wflags.$(compiler_name).$(compiler_version))
How can I turn on (literally) ALL of GCC's warnings?
You can't.
The manual for GCC 4.4.0 is only comprehensive for that version, but it does list all the possible warnings for 4.4.0. They're not all on the page you link to though. For instance, some language-specific options are on the pages for C++ options or Objective-C options. To find them all, you're better off looking at the Options Summary
Turning on everything would include -Wdouble-promotion
which is only relevant on CPUs with a 32-bit single-precision floating-point unit which implements float
in hardware, but emulates double
in software. Doing calculations as double
would use the software emulation and be slower. That's relevant for some embedded CPUs, but completely irrelevant for modern desktop CPUs with hardware support for 64-bit floating-point.
Another warning that's not usually useful is -Wtraditional
, which warns about perfectly well formed code that has a different meaning (or doesn't work) in traditional C, e.g., "string " "concatenation"
, or ISO C function definitions! Do you really care about compatibility with 30 year old compilers? Do you really want a warning for writing int inc(int i) { return i+1; }
?
I think -Weffc++
is too noisy to be useful. It's based on the outdated first edition of Effective C++ and warns about constructs which are perfectly valid C++ (and for which the guidelines changed in later editions of the book). I don't want to be warned that I haven't initialized a std::string
member in my constructor; it has a default constructor that does exactly what I want. Why should I write m_str()
to call it? The -Weffc++
warnings that would be helpful are too difficult for the compiler to detect accurately (giving false negatives), and the ones that aren't useful, such as initializing all members explicitly, just produce too much noise, giving false positives.
Luc Danton provided a great example of useless warnings from -Waggregate-return
that almost certainly never makes sense for C++ code.
I.e., you don't really want all warnings; you just think you do.
Go through the manual, read about them, decide which you might want to enable, and try them. Reading your compiler's manual is a Good ThingTM anyway, taking a shortcut and enabling warnings you don't understand is not a very good idea, especially if it's to avoid having to RTFM.
Anyone who just turns on everything is probably either doing so because they're clueless because or a pointy-haired boss said "no warnings."
Some warnings are important, and some aren't. You have to be discriminating or you mess up your program. Consider, for instance, -Wdouble-promotion
. If you're working on an embedded system you might want this; if you're working on a desktop system you probably don't. And do you want -Wtraditional
? I doubt it.
See also -Wall-all to enable all warnings which is closed as WONTFIX.
In response to DevSolar's complaint about makefiles needing to use different warnings depending on compiler version, if -Wall -Wextra
isn't suitable then it's not difficult to use compiler-specific and version-specific CFLAGS:
compiler_name := $(notdir $(CC))
ifeq ($(compiler_name),gcc)
compiler_version := $(basename $(shell $(CC) -dumpversion))
endif
ifeq ($(compile_name),clang)
compiler_version := $(shell $(CC) --version | awk 'NR==1{print $$3}')
endif
# ...
wflags.gcc.base := -Wall -Wextra
wflags.gcc.4.7 := -Wzero-as-null-pointer-constant
wflags.gcc.4.8 := $(wflags.gcc.4.7)
wflags.clang.base := -Wall -Wextra
wflags.clang.3.2 := -Weverything
CFLAGS += $(wflags.$(compiler_name).base) $(wflags.$(compiler_name).$(compiler_version))
What does the -DQT_WEBKIT flag mean in g ++?
The -D
option is a preprocessor option and is documented in section 3.13 Preprocessor Options of the GCC Manual. (You can also always just run gcc --help
or g++ --help
to get help for the options.)
The -D<name>[=<value>]
option #define
s the macro <name>
to have the value <value>
(or 1
if the value <value>
is not provided).
So, in your case, it defines the three macros _REENTRANT
, QT_WEBKIT
, and D_TESTR
to the value 1
, in other words, it is exactly equivalent to having
#define _REENTRANT 1
#define QT_WEBKIT 1
#define D_TESTR 1
prepended to the file being compiled.
How can I turn on (literally) ALL of GCC's warnings?
You can't.
The manual for GCC 4.4.0 is only comprehensive for that version, but it does list all the possible warnings for 4.4.0. They're not all on the page you link to though. For instance, some language-specific options are on the pages for C++ options or Objective-C options. To find them all, you're better off looking at the Options Summary
Turning on everything would include -Wdouble-promotion
which is only relevant on CPUs with a 32-bit single-precision floating-point unit which implements float
in hardware, but emulates double
in software. Doing calculations as double
would use the software emulation and be slower. That's relevant for some embedded CPUs, but completely irrelevant for modern desktop CPUs with hardware support for 64-bit floating-point.
Another warning that's not usually useful is -Wtraditional
, which warns about perfectly well formed code that has a different meaning (or doesn't work) in traditional C, e.g., "string " "concatenation"
, or ISO C function definitions! Do you really care about compatibility with 30 year old compilers? Do you really want a warning for writing int inc(int i) { return i+1; }
?
I think -Weffc++
is too noisy to be useful. It's based on the outdated first edition of Effective C++ and warns about constructs which are perfectly valid C++ (and for which the guidelines changed in later editions of the book). I don't want to be warned that I haven't initialized a std::string
member in my constructor; it has a default constructor that does exactly what I want. Why should I write m_str()
to call it? The -Weffc++
warnings that would be helpful are too difficult for the compiler to detect accurately (giving false negatives), and the ones that aren't useful, such as initializing all members explicitly, just produce too much noise, giving false positives.
Luc Danton provided a great example of useless warnings from -Waggregate-return
that almost certainly never makes sense for C++ code.
I.e., you don't really want all warnings; you just think you do.
Go through the manual, read about them, decide which you might want to enable, and try them. Reading your compiler's manual is a Good ThingTM anyway, taking a shortcut and enabling warnings you don't understand is not a very good idea, especially if it's to avoid having to RTFM.
Anyone who just turns on everything is probably either doing so because they're clueless because or a pointy-haired boss said "no warnings."
Some warnings are important, and some aren't. You have to be discriminating or you mess up your program. Consider, for instance, -Wdouble-promotion
. If you're working on an embedded system you might want this; if you're working on a desktop system you probably don't. And do you want -Wtraditional
? I doubt it.
See also -Wall-all to enable all warnings which is closed as WONTFIX.
In response to DevSolar's complaint about makefiles needing to use different warnings depending on compiler version, if -Wall -Wextra
isn't suitable then it's not difficult to use compiler-specific and version-specific CFLAGS:
compiler_name := $(notdir $(CC))
ifeq ($(compiler_name),gcc)
compiler_version := $(basename $(shell $(CC) -dumpversion))
endif
ifeq ($(compile_name),clang)
compiler_version := $(shell $(CC) --version | awk 'NR==1{print $$3}')
endif
# ...
wflags.gcc.base := -Wall -Wextra
wflags.gcc.4.7 := -Wzero-as-null-pointer-constant
wflags.gcc.4.8 := $(wflags.gcc.4.7)
wflags.clang.base := -Wall -Wextra
wflags.clang.3.2 := -Weverything
CFLAGS += $(wflags.$(compiler_name).base) $(wflags.$(compiler_name).$(compiler_version))
Related Topics
Why Vector≪Bool≫::Reference Doesn't Return Reference to Bool
How Std::Unordered_Map Is Implemented
Is the Return Type Part of the Function Signature
Normal Mapping Gone Horribly Wrong
Are Members of a C++ Struct Initialized to 0 by Default
Cmake Cannot Find Library Using "Link_Directories"
Are C++ Reads and Writes of an Int Atomic
Elegant Solution to Duplicate, Const and Non-Const, Getters
Error Lnk2019: Unresolved External Symbol _Winmain@16 Referenced in Function _Tmaincrtstartup
How to Append Text to a Text File in C++
Running My Program Says "Bash: ./Program Permission Denied"
How to Calculate Execution Time of a Code Snippet in C++
How to Use Stringstream to Separate Comma Separated Strings
Using Local Classes With Stl Algorithms
C++ Semantics of 'Static Const' VS 'Const'
How to Get Rid of 'Deprecated Conversion from String Constant to 'Char*'' Warnings in Gcc