18.5. Compiler WarningsYou'll get two types of complaints from GCC when compiling a C program. Error messages refer to problems that make your program impossible to compile. Warnings refer to conditions in your program that you might want to know about and changefor stricter conformance to a given standard, for examplebut that do not prevent the compiler from finishing its job. You may be able to compile and run a program in spite of some compiler warningsalthough that doesn't mean it's a good idea to do so. GCC gives you very fine control over the warning messages that it provides. For example, if you don't like the distinction between errors and warnings, you can use the -Werror option to make GCC stop compiling on any warning, as if it were an error. Other options let you request warnings about archaic or nonstandard usage, and about many kinds of C constructs in your programs that are considered hazardous, ambiguous, or sloppy. You can enable most of GCC's warnings individually using options that begin with -W. For example, the option -Wswitch-default causes GCC to produce a warning message whenever you use a switch statement without a default label, and -Wsequence-point provides a warning when the value of an expression between two sequence points depends on a subexpression that is modified in the same interval (see "Side Effects and Sequence Points" in Chapter 5). The easiest way to request these and many other warnings from GCC is to use the command-line option -Wall. However, the name of this option is somewhat misleading: -Wall does not enable all of the individual -W options. Quite a few more must be asked for specifically by name, such as -Wshadow: this option gives you a warning whenever you define a variable with block scope that has the same name as, and thus "shadows," another variable with a larger scope. Such warnings are not among those produced by -Wall. If you use the -Wall option but want to disable a subset of the warnings it causes, you can insert no- after the -W in the names of individual warning options. Thus -Wno-switch-default turns off warnings about switch statements without default. Furthermore, the -w option (that's a lowercase w) anywhere on the command line turns off all warnings. The option -WexTRa (formerly named simply -W, with no suffix) adds warnings about a number of legal but questionable expressions, such as testing whether an unsigned value is negative or non-negative: unsigned int u; /* ... */ if ( u < 0 ) { /* ... this block is never executed ... */ } The -Wextra option also warns expressions that have no side effects and whose value is discarded. The full set of conditions it checks for is described in the GCC manual. Furthermore, if you are updating older programs, you may want to use -WTRaditional to request warnings about constructs that have different meanings in old-style C and ISO standard C, such as a string literal in a macro body that contains the macro's argument: #define printerror(x) fputs("x\n", stderr) In older, "traditional" C, this macro would work as intended, but ISO standard C, it would print the letter "x" and a newline character each time you use it. Hence -WTRaditional would generate a warning for this line: file:line:column: warning: macro argument "x" would be stringified in traditional C |