Next: Automating the Process with
Up: Compiling with GCC
Previous: Common Command-line Options
Error Checking and Warnings
GCC boasts a whole class of error-checking, warning-generating, command-line options. These include -ansi, -pedantic, -pedantic- errors, and -Wall. To begin with,
- -pedantic tells GCC to issue all warnings demanded by strict ANSI/ISO standard C. Any program using forbidden extensions, such as those supported by GCC, will be rejected.
- -pedantic-errors behaves similarly, except that it emits errors rather than warnings.
- -ansi, finally, turns off GNU extensions that do not comply with the standard.
None of these options, however, guarantee that your code, when compiled without error using any or all of these options, is 100 percent ANSI/ISO-compliant.
Consider http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/pedant.cpedant.c , an example of very bad programming form. It declares main() as returning void, when in fact main() returns int, and it uses the GNU extension long long to declare a 64-bit integer.
// pedant.c - use -ansi, -pedantic or -pedantic-errors
#include <stdio.h>
void main(void)
{
long long int i = 01;
fprintf(stdout, "This is a non-conforming C program\n");
}
Using
%gcc pedant.c -o pedant
this code compiles without complaint.
- First, try to compile it using -ansi:
%gcc -ansi pedant.c -o pedant
Again, no complaint. The lesson here is that -ansi forces GCC to emit the diagnostic messages required by the standard. It does not insure that your code is ANSI C compliant. The program compiled despite the deliberately incorrect declaration of main().
- Now, -pedantic:
%gcc -pedantic pedant.c -o pedant
pedant.c: In function `main':
pedant.c:9: warning: ANSI C does not support `long long'
The code compiles, despite the emitted warning.
- With -pedantic- errors, however, it does not compile. GCC stops after emitting the error diagnostic:
%gcc -pedantic-errors pedant.c -o pedant
pedant.c: In function `main':
pedant.c:9: ANSI C does not support `long long'
To reiterate, the -ansi, -pedantic, and -pedantic-errors compiler options do not insure ANSI/ISO-compliant code. They merely help you along the road.
Some users try to use -pedantic to check programs for strict ANSI C conformance.
Next: Automating the Process with
Up: Compiling with GCC
Previous: Common Command-line Options
Cem Ozdogan
2007-02-19