Next: , Previous: Handling floating point exceptions, Up: Debugging Numerical Programs


A.4 GCC warning options for numerical programs

Writing reliable numerical programs in C requires great care. The following GCC warning options are recommended when compiling numerical programs:

     gcc -ansi -pedantic -Werror -Wall -W
       -Wmissing-prototypes -Wstrict-prototypes
       -Wconversion -Wshadow -Wpointer-arith
       -Wcast-qual -Wcast-align
       -Wwrite-strings -Wnested-externs
       -fshort-enums -fno-common -Dinline= -g -O2

For details of each option consult the manual Using and Porting GCC. The following table gives a brief explanation of what types of errors these options catch.

-ansi -pedantic
Use ANSI C, and reject any non-ANSI extensions. These flags help in writing portable programs that will compile on other systems.
-Werror
Consider warnings to be errors, so that compilation stops. This prevents warnings from scrolling off the top of the screen and being lost. You won't be able to compile the program until it is completely warning-free.
-Wall
This turns on a set of warnings for common programming problems. You need -Wall, but it is not enough on its own.
-O2
Turn on optimization. The warnings for uninitialized variables in -Wall rely on the optimizer to analyze the code. If there is no optimization then these warnings aren't generated.
-W
This turns on some extra warnings not included in -Wall, such as missing return values and comparisons between signed and unsigned integers.
-Wmissing-prototypes -Wstrict-prototypes
Warn if there are any missing or inconsistent prototypes. Without prototypes it is harder to detect problems with incorrect arguments.
-Wconversion
The main use of this option is to warn about conversions from signed to unsigned integers. For example, unsigned int x = -1. If you need to perform such a conversion you can use an explicit cast.
-Wshadow
This warns whenever a local variable shadows another local variable. If two variables have the same name then it is a potential source of confusion.
-Wpointer-arith -Wcast-qual -Wcast-align
These options warn if you try to do pointer arithmetic for types which don't have a size, such as void, if you remove a const cast from a pointer, or if you cast a pointer to a type which has a different size, causing an invalid alignment.
-Wwrite-strings
This option gives string constants a const qualifier so that it will be a compile-time error to attempt to overwrite them.
-fshort-enums
This option makes the type of enum as short as possible. Normally this makes an enum different from an int. Consequently any attempts to assign a pointer-to-int to a pointer-to-enum will generate a cast-alignment warning.
-fno-common
This option prevents global variables being simultaneously defined in different object files (you get an error at link time). Such a variable should be defined in one file and referred to in other files with an extern declaration.
-Wnested-externs
This warns if an extern declaration is encountered within a function.
-Dinline=
The inline keyword is not part of ANSI C. Thus if you want to use -ansi with a program which uses inline functions you can use this preprocessor definition to remove the inline keywords.
-g
It always makes sense to put debugging symbols in the executable so that you can debug it using gdb. The only effect of debugging symbols is to increase the size of the file, and you can use the strip command to remove them later if necessary.