| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the `-c' option says not to run the linker. Then the output consists of object files output by the assembler.
Other options are passed on to one stage of processing. Some options control the preprocessor and others the compiler itself. Yet other options control the assembler and linker; most of these are not documented here, since you rarely need to use any of them.
Most of the command line options that you can use with GCC are useful for C programs; when an option is only useful with another language (usually C++), the explanation says so explicitly. If the description for a particular option does not mention a source language, you can use that option with all supported languages.
See section Compiling C++ Programs, for a summary of special options for compiling C++ programs.
The gcc program accepts options and file names as operands. Many
options have multi-letter names; therefore multiple single-letter options
may not be grouped: `-dr' is very different from `-d
-r'.
You can mix options and other arguments. For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify `-L' more than once, the directories are searched in the order specified.
Many options have long names starting with `-f' or with `-W'---for example, `-fforce-mem', `-fstrength-reduce', `-Wformat' and so on. Most of these have both positive and negative forms; the negative form of `-ffoo' would be `-fno-foo'. This manual documents only one of these two forms, whichever one is not the default.
See section Option Index, for an index to GCC's options.
3.1 Option Summary Brief list of all options, without explanations. 3.2 Options Controlling the Kind of Output Controlling the kind of output: an executable, object files, assembler files, or preprocessed source. 3.3 Compiling C++ Programs Compiling C++ programs. 3.4 Options Controlling C Dialect Controlling the variant of C language compiled. 3.5 Options Controlling C++ Dialect Variations on C++. 3.6 Options Controlling Objective-C Dialect Variations on Objective-C. 3.7 Options to Control Diagnostic Messages Formatting Controlling how diagnostics should be formatted. 3.8 Options to Request or Suppress Warnings How picky should the compiler be? 3.9 Options for Debugging Your Program or GCC Symbol tables, measurements, and debugging dumps. 3.10 Options That Control Optimization How much optimization? 3.11 Options Controlling the Preprocessor Controlling header files and macro definitions. Also, getting dependency information for Make. 3.12 Passing Options to the Assembler Passing options to the assembler. 3.13 Options for Linking Specifying libraries and so on. 3.14 Options for Directory Search Where to find header files and libraries. Where to find the compiler executable files. 3.15 Specifying subprocesses and the switches to pass to them How to pass switches to sub-processes. 3.16 Specifying Target Machine and Compiler Version Running a cross-compiler, or an old version of GCC. 3.17 Hardware Models and Configurations Specifying minor hardware or convention variations, such as 68010 vs 68020. 3.18 Options for Code Generation Conventions Specifying conventions for function calls, data layout and register usage. 3.19 Environment Variables Affecting GCC Env vars that affect GCC. 3.20 Running Protoize Automatically adding or removing function prototypes.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is a summary of all the options, grouped by type. Explanations are in the following sections.
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
{
|
M68hc1x Options
{
|
VAX Options
{
|
SPARC Options
{
|
Convex Options
{
|
AMD29K Options
{
|
ARM Options
{
|
MN10200 Options
{
|
MN10300 Options
{
|
M32R/D Options
{
|
M88K Options
{
|
RS/6000 and PowerPC Options
{
|
RT Options
{
|
MIPS Options
{
|
i386 Options
{
|
HPPA Options
{
|
Intel 960 Options
{
|
DEC Alpha Options
{
|
Clipper Options
{
|
H8/300 Options
{
|
SH Options
{
|
System V Options
{
|
ARC Options
{
|
TMS320C3x/C4x Options
{
|
V850 Options
{
|
NS32K Options
{
|
AVR Options
{
|
MCore Options
{
|
IA-64 Options
{
|
S/390 and zSeries Options
{
|
Xtensa Options
{
|
{
|
3.2 Options Controlling the Kind of Output Controlling the kind of output: an executable, object files, assembler files, or preprocessed source. 3.4 Options Controlling C Dialect Controlling the variant of C language compiled. 3.5 Options Controlling C++ Dialect Variations on C++. 3.6 Options Controlling Objective-C Dialect Variations on Objective-C. 3.7 Options to Control Diagnostic Messages Formatting Controlling how diagnostics should be formatted. 3.8 Options to Request or Suppress Warnings How picky should the compiler be? 3.9 Options for Debugging Your Program or GCC Symbol tables, measurements, and debugging dumps. 3.10 Options That Control Optimization How much optimization? 3.11 Options Controlling the Preprocessor Controlling header files and macro definitions. Also, getting dependency information for Make. 3.12 Passing Options to the Assembler Passing options to the assembler. 3.13 Options for Linking Specifying libraries and so on. 3.14 Options for Directory Search Where to find header files and libraries. Where to find the compiler executable files. 3.15 Specifying subprocesses and the switches to pass to them How to pass switches to sub-processes. 3.16 Specifying Target Machine and Compiler Version Running a cross-compiler, or an old version of GCC.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Compilation can involve up to four stages: preprocessing, compilation proper, assembly and linking, always in that order. The first three stages apply to an individual source file, and end by producing an object file; linking combines all the object files (those newly compiled, and those specified as input) into an executable file.
For any given input file, the file name suffix determines what kind of compilation is done:
file.c
file.i
file.ii
file.m
file.mi
file.h
file.cc
file.cp
file.cxx
file.cpp
file.c++
file.C
file.f
file.for
file.FOR
file.F
file.fpp
file.FPP
file.r
See section `Options Controlling the Kind of Output' in Using and Porting GNU Fortran, for more details of the handling of Fortran input files.
file.s
file.S
other
You can specify the input language explicitly with the `-x' option:
-x language
c c-header cpp-output c++ c++-cpp-output objective-c objc-cpp-output assembler assembler-with-cpp f77 f77-cpp-input ratfor java |
-x none
-pass-exit-codes
gcc program will exit with the code of 1 if any
phase of the compiler returns a non-success return code. If you specify
`-pass-exit-codes', the gcc program will instead return with
numerically highest error produced by any phase that returned an error
indication.
If you only want some of the stages of compilation, you can use
`-x' (or filename suffixes) to tell gcc where to start, and
one of the options `-c', `-S', or `-E' to say where
gcc is to stop. Note that some combinations (for example,
`-x cpp-output -E') instruct gcc to do nothing at all.
-c
By default, the object file name for a source file is made by replacing the suffix `.c', `.i', `.s', etc., with `.o'.
Unrecognized input files, not requiring compilation or assembly, are ignored.
-S
By default, the assembler file name for a source file is made by replacing the suffix `.c', `.i', etc., with `.s'.
Input files that don't require compilation are ignored.
-E
Input files which don't require preprocessing are ignored.
-o file
Since only one output file can be specified, it does not make sense to use `-o' when compiling more than one input file, unless you are producing an executable file as output.
If `-o' is not specified, the default is to put an executable file in `a.out', the object file for `source.suffix' in `source.o', its assembler file in `source.s', and all preprocessed C source on standard output.
-v
-pipe
--help
gcc. If the `-v' option is also specified
then `--help' will also be passed on to the various processes
invoked by gcc, so that they can display the command line options
they accept. If the `-W' option is also specified then command
line options which have no documentation associated with them will also
be displayed.
--target-help
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
C++ source files conventionally use one of the suffixes `.C',
`.cc', `.cpp', `.c++', `.cp', or `.cxx';
preprocessed C++ files use the suffix `.ii'. GCC recognizes
files with these names and compiles them as C++ programs even if you
call the compiler the same way as for compiling C programs (usually with
the name gcc).
However, C++ programs often require class libraries as well as a
compiler that understands the C++ language--and under some
circumstances, you might want to compile programs from standard input,
or otherwise without a suffix that flags them as C++ programs.
g++ is a program that calls GCC with the default language
set to C++, and automatically specifies linking against the C++
library. On many systems, g++ is also
installed with the name c++.
When you compile C++ programs, you may specify many of the same command-line options that you use for compiling programs in any language; or command-line options meaningful for C and related languages; or options that are meaningful only for C++ programs. See section Options Controlling C Dialect, for explanations of options for languages related to C. See section Options Controlling C++ Dialect, for explanations of options that are meaningful only for C++ programs.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following options control the dialect of C (or languages derived from C, such as C++ and Objective C) that the compiler accepts:
-ansi
This turns off certain features of GCC that are incompatible with ISO
C89 (when compiling C code), or of standard C++ (when compiling C++ code),
such as the asm and typeof keywords, and
predefined macros such as unix and vax that identify the
type of system you are using. It also enables the undesirable and
rarely used ISO trigraph feature. For the C compiler,
it disables recognition of C++ style `//' comments as well as
the inline keyword.
The alternate keywords __asm__, __extension__,
__inline__ and __typeof__ continue to work despite
`-ansi'. You would not want to use them in an ISO C program, of
course, but it is useful to put them in header files that might be included
in compilations done with `-ansi'. Alternate predefined macros
such as __unix__ and __vax__ are also available, with or
without `-ansi'.
The `-ansi' option does not cause non-ISO programs to be rejected gratuitously. For that, `-pedantic' is required in addition to `-ansi'. See section 3.8 Options to Request or Suppress Warnings.
The macro __STRICT_ANSI__ is predefined when the `-ansi'
option is used. Some header files may notice this macro and refrain
from declaring certain functions or defining certain macros that the
ISO standard doesn't call for; this is to avoid interfering with any
programs that might use these names for other things.
Functions which would normally be built in but do not have semantics
defined by ISO C (such as alloca and ffs) are not built-in
functions with `-ansi' is used. See section Other built-in functions provided by GCC, for details of the functions
affected.
-std=
Even when this option is not specified, you can still use some of the
features of newer standards in so far as they do not conflict with
previous C standards. For example, you may use __restrict__ even
when `-std=c99' is not specified.
The `-std' options specifying some version of ISO C have the same
effects as `-ansi', except that features that were not in ISO C89
but are in the specified version (for example, `//' comments and
the inline keyword in ISO C99) are not disabled.
See section Language Standards Supported by GCC, for details of these standard versions.
-aux-info filename
Besides declarations, the file indicates, in comments, the origin of each declaration (source file and line), whether the declaration was implicit, prototyped or unprototyped (`I', `N' for new or `O' for old, respectively, in the first character after the line number and the colon), and whether it came from a declaration or a definition (`C' or `F', respectively, in the following character). In the case of function definitions, a K&R-style list of arguments followed by their declarations is also provided, inside comments, after the declaration.
-fno-asm
asm, inline or typeof as a
keyword, so that code can use these words as identifiers. You can use
the keywords __asm__, __inline__ and __typeof__
instead. `-ansi' implies `-fno-asm'.
In C++, this switch only affects the typeof keyword, since
asm and inline are standard keywords. You may want to
use the `-fno-gnu-keywords' flag instead, which has the same
effect. In C99 mode (`-std=c99' or `-std=gnu99'), this
switch only affects the asm and typeof keywords, since
inline is a standard keyword in ISO C99.
-fno-builtin
GCC normally generates special code to handle certain built-in functions
more efficiently; for instance, calls to alloca may become single
instructions that adjust the stack directly, and calls to memcpy
may become inline copy loops. The resulting code is often both smaller
and faster, but since the function calls no longer appear as such, you
cannot set a breakpoint on those calls, nor can you change the behavior
of the functions by linking with a different library.
In C++, `-fno-builtin' is always in effect. The `-fbuiltin'
option has no effect. Therefore, in C++, the only way to get the
optimization benefits of built-in functions is to call the function
using the `__builtin_' prefix. The GNU C++ Standard Library uses
built-in functions to implement many functions (like
std::strchr), so that you automatically get efficient code.
-fhosted
Assert that compilation takes place in a hosted environment. This implies
`-fbuiltin'. A hosted environment is one in which the
entire standard library is available, and in which main has a return
type of int. Examples are nearly everything except a kernel.
This is equivalent to `-fno-freestanding'.
-ffreestanding
Assert that compilation takes place in a freestanding environment. This
implies `-fno-builtin'. A freestanding environment
is one in which the standard library may not exist, and program startup may
not necessarily be at main. The most obvious example is an OS kernel.
This is equivalent to `-fno-hosted'.
See section Language Standards Supported by GCC, for details of freestanding and hosted environments.
-trigraphs
-traditional
extern declarations take effect globally even if they
are written inside of a function definition. This includes implicit
declarations of functions.
typeof, inline, signed, const
and volatile are not recognized. (You can still use the
alternative keywords such as __typeof__, __inline__, and
so on.)
unsigned short and unsigned char promote
to unsigned int.
register are preserved by
longjmp. Ordinarily, GNU C follows ISO C: automatic variables
not declared volatile may be clobbered.
You may wish to use `-fno-builtin' as well as `-traditional' if your program uses names that are normally GNU C built-in functions for other purposes of its own.
You cannot use `-traditional' if you include any header files that rely on ISO C features. Some vendors are starting to ship systems with ISO C header files and you cannot use `-traditional' on such systems to compile files that include any system headers.
The `-traditional' option also enables `-traditional-cpp', which is described next.
-traditional-cpp
__STDC__ is not defined when you use
`-traditional', but __GNUC__ is (since the GNU extensions
which __GNUC__ indicates are not affected by
`-traditional'). If you need to write header files that work
differently depending on whether `-traditional' is in use, by
testing both of these predefined macros you can distinguish four
situations: GNU C, traditional GNU C, other ISO C compilers, and other
old C compilers. The predefined macro __STDC_VERSION__ is also
not defined when you use `-traditional'. See section `Standard Predefined Macros' in The C Preprocessor,
for more discussion of these and other predefined macros.
-fcond-mismatch
-funsigned-char
char be unsigned, like unsigned char.
Each kind of machine has a default for what char should
be. It is either like unsigned char by default or like
signed char by default.
Ideally, a portable program should always use signed char or
unsigned char when it depends on the signedness of an object.
But many programs have been written to use plain char and
expect it to be signed, or expect it to be unsigned, depending on the
machines they were written for. This option, and its inverse, let you
make such a program work with the opposite default.
The type char is always a distinct type from each of
signed char or unsigned char, even though its behavior
is always just like one of those two.
-fsigned-char
char be signed, like signed char.
Note that this is equivalent to `-fno-unsigned-char', which is the negative form of `-funsigned-char'. Likewise, the option `-fno-signed-char' is equivalent to `-funsigned-char'.
-fsigned-bitfields
-funsigned-bitfields
-fno-signed-bitfields
-fno-unsigned-bitfields
signed or unsigned. By
default, such a bit-field is signed, because this is consistent: the
basic integer types such as int are signed types.
However, when `-traditional' is used, bit-fields are all unsigned no matter what.
-fwritable-strings
Writing into string constants is a very bad idea; "constants" should be constant.
-fallow-single-precision
Traditional K&R C promotes all floating point operations to double precision, regardless of the sizes of the operands. On the architecture for which you are compiling, single precision may be faster than double precision. If you must use `-traditional', but want to use single precision operations when the operands are single precision, use this option. This option has no effect when compiling with ISO or GNU C conventions (the default).
-fshort-wchar
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the command-line options that are only meaningful
for C++ programs; but you can also use most of the GNU compiler options
regardless of what language your program is in. For example, you
might compile a file firstClass.C like this:
g++ -g -frepo -O -c firstClass.C |
In this example, only `-frepo' is an option meant only for C++ programs; you can use the other options with any language supported by GCC.
Here is a list of options that are only for compiling C++ programs:
-fno-access-control
-fcheck-new
operator new is non-null
before attempting to modify the storage allocated. The current Working
Paper requires that operator new never return a null pointer, so
this check is normally unnecessary.
An alternative to using this option is to specify that your
operator new does not throw any exceptions; if you declare it
`throw()', G++ will check the return value. See also `new
(nothrow)'.
-fconserve-space
main() has
completed, you may have an object that is being destroyed twice because
two definitions were merged.
This option is no longer useful on most targets, now that support has been added for putting variables into BSS without making them common.
-fno-const-strings
char * instead of type const
char *. By default, G++ uses type const char * as required by
the standard. Even if you use `-fno-const-strings', you cannot
actually modify the value of a string constant, unless you also use
`-fwritable-strings'.
This option might be removed in a future release of G++. For maximum
portability, you should structure your code so that it works with
string constants that have type const char *.
-fdollars-in-identifiers
-fno-elide-constructors
-fno-enforce-eh-specs
-fexternal-templates
This option is deprecated.
-falt-external-templates
This option is deprecated.
-ffor-scope
-fno-for-scope
The default if neither flag is given to follow the standard, but to allow and give a warning for old-style code that would otherwise be invalid, or have different behavior.
-fno-gnu-keywords
typeof as a keyword, so that code can use this
word as an identifier. You can use the keyword __typeof__ instead.
`-ansi' implies `-fno-gnu-keywords'.
-fno-implicit-templates
-fno-implicit-inline-templates
-fno-implement-inlines
-fms-extensions
-fno-nonansi-builtins
ffs, alloca, _exit,
index, bzero, conjf, and other related functions.
-fno-operator-names
and, bitand,
bitor, compl, not, or and xor as
synonyms as keywords.
-fno-optional-diags
-fpermissive
-frepo
-fno-rtti
-fstats
-ftemplate-depth-n
-fuse-cxa-atexit
__cxa_atexit function rather than the atexit function.
This option is required for fully standards-compliant handling of static
destructors, but will only work if your C library supports
__cxa_atexit.
-fno-weak
-nostdinc++
In addition, these optimization, warning, and code generation options have meanings only for C++ programs:
-fno-default-inline
-Wctor-dtor-privacy (C++ only)
-Wnon-virtual-dtor (C++ only)
-Wreorder (C++ only)
struct A {
int i;
int j;
A(): j (0), i (1) { }
};
|
Here the compiler will warn that the member initializers for `i' and `j' will be rearranged to match the declaration order of the members.
The following `-W...' options are not affected by `-Wall'.
-Weffc++ (C++ only)
-Wno-deprecated (C++ only)
-Wno-non-template-friend (C++ only)
-Wold-style-cast (C++ only)
-Woverloaded-virtual (C++ only)
-Wno-pmf-conversions (C++ only)
-Wsign-promo (C++ only)
-Wsynth (C++ only)
struct A {
operator int ();
A& operator = (int);
};
main ()
{
A a,b;
a = b;
}
|
In this example, G++ will synthesize a default `A& operator = (const A&);', while cfront will use the user-defined `operator ='.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the command-line options that are only meaningful
for Objective-C programs; but you can also use most of the GNU compiler
options regardless of what language your program is in. For example,
you might compile a file some_class.m like this:
gcc -g -fgnu-runtime -O -c some_class.m |
In this example, only `-fgnu-runtime' is an option meant only for Objective-C programs; you can use the other options with any language supported by GCC.
Here is a list of options that are only for compiling Objective-C programs:
-fconstant-string-class=class-name
@"...". The default
class name is NXConstantString.
-fgnu-runtime
-fnext-runtime
-gen-decls
-Wno-protocol
-Wselector
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Traditionally, diagnostic messages have been formatted irrespective of the output device's aspect (e.g. its width, ...). The options described below can be used to control the diagnostic messages formatting algorithm, e.g. how many characters per line, how often source location information should be reported. Right now, only the C++ front end can honor these options. However it is expected, in the near future, that the remaining front ends would be able to digest them correctly.
-fmessage-length=n
g++ and 0 for the rest of
the front ends supported by GCC. If n is zero, then no
line-wrapping will be done; each error message will appear on a single
line.
-fdiagnostics-show-location=once
-fdiagnostics-show-location=every-line
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Warnings are diagnostic messages that report constructions which are not inherently erroneous but which are risky or suggest there may have been an error.
You can request many specific warnings with options beginning `-W', for example `-Wimplicit' to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning `-Wno-' to turn off warnings; for example, `-Wno-implicit'. This manual lists only one of the two forms, whichever is not the default.
These options control the amount and kinds of warnings produced by GCC:
-fsyntax-only
-pedantic
Valid ISO C and ISO C++ programs should compile properly with or without this option (though a rare few will require `-ansi' or a `-std' option specifying the required version of ISO C). However, without this option, certain GNU extensions and traditional C and C++ features are supported as well. With this option, they are rejected.
`-pedantic' does not cause warning messages for use of the
alternate keywords whose names begin and end with `__'. Pedantic
warnings are also disabled in the expression that follows
__extension__. However, only system header files should use
these escape routes; application programs should avoid them.
See section 5.39 Alternate Keywords.
Some users try to use `-pedantic' to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all--only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.
A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from `-pedantic'. We don't have plans to support such a feature in the near future.
Where the standard specified with `-std' represents a GNU extended dialect of C, such as `gnu89' or `gnu99', there is a corresponding base standard, the version of ISO C on which the GNU extended dialect is based. Warnings from `-pedantic' are given where they are required by the base standard. (It would not make sense for such warnings to be given only for features not in the specified GNU C dialect, since by definition the GNU dialects of C include all features the compiler supports with the given option, and there would be nothing to warn about.)
-pedantic-errors
-w
-Wno-import
-Wchar-subscripts
char. This is a common cause
of error, as programmers often forget that this type is signed on some
machines.
-Wcomment
-Wformat
printf and scanf, etc., to make sure that
the arguments supplied have types appropriate to the format string
specified, and that the conversions specified in the format string make
sense. This includes standard functions, and others specified by format
attributes (see section 5.26 Declaring Attributes of Functions), in the printf,
scanf, strftime and strfmon (an X/Open extension,
not in the C standard) families.
The formats are checked against the format features supported by GNU
libc version 2.2. These include all ISO C89 and C99 features, as well
as features from the Single Unix Specification and some BSD and GNU
extensions. Other library implementations may not support all these
features; GCC does not support warning about features that go beyond a
particular library's limitations. However, if `-pedantic' is used
with `-Wformat', warnings will be given about format features not
in the selected standard version (but not for strfmon formats,
since those are not in any version of the C standard). See section Options Controlling C Dialect.
`-Wformat' is included in `-Wall'. For more control over some aspects of format checking, the options `-Wno-format-y2k', `-Wno-format-extra-args', `-Wformat-nonliteral', `-Wformat-security' and `-Wformat=2' are available, but are not included in `-Wall'.
-Wno-format-y2k
strftime
formats which may yield only a two-digit year.
-Wno-format-extra-args
printf or scanf format function. The C standard specifies
that such arguments are ignored.
-Wformat-nonliteral
va_list.
-Wformat-security
printf and scanf functions where the
format string is not a string literal and there are no format arguments,
as in printf (foo);. This may be a security hole if the format
string came from untrusted input and contains `%n'. (This is
currently a subset of what `-Wformat-nonliteral' warns about, but
in future warnings may be added to `-Wformat-security' that are not
included in `-Wformat-nonliteral'.)
-Wformat=2
-Wimplicit-int
-Wimplicit-function-declaration
-Werror-implicit-function-declaration
-Wimplicit
-Wmain
-Wmissing-braces
int a[2][2] = { 0, 1, 2, 3 };
int b[2][2] = { { 0, 1 }, { 2, 3 } };
|
-Wmultichar
-Wparentheses
Also warn about constructions where there may be confusion to which
if statement an else branch belongs. Here is an example of
such a case:
{
if (a)
if (b)
foo ();
else
bar ();
}
|
In C, every else branch belongs to the innermost possible if
statement, which in this example is if (b). This is often not
what the programmer expected, as illustrated in the above example by
indentation the programmer chose. When there is the potential for this
confusion, GCC will issue a warning when this flag is specified.
To eliminate the warning, add explicit braces around the innermost
if statement so there is no way the else could belong to
the enclosing if. The resulting code would look like this:
{
if (a)
{
if (b)
foo ();
else
bar ();
}
}
|
-Wsequence-point
The C standard defines the order in which expressions in a C program are
evaluated in terms of sequence points, which represent a partial
ordering between the execution of parts of the program: those executed
before the sequence point, and those executed after it. These occur
after the evaluation of a full expression (one which is not part of a
larger expression), after the evaluation of the first operand of a
&&, ||, ? : or , (comma) operator, before a
function is called (but after the evaluation of its arguments and the
expression denoting the called function), and in certain other places.
Other than as expressed by the sequence point rules, the order of
evaluation of subexpressions of an expression is not specified. All
these rules describe only a partial order rather than a total order,
since, for example, if two functions are called within one expression
with no sequence point between them, the order in which the functions
are called is not specified. However, the standards committee have
ruled that function calls do not overlap.
It is not specified when between sequence points modifications to the values of objects take effect. Programs whose behavior depends on this have undefined behavior; the C standard specifies that "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.". If a program breaks these rules, the results on any particular implementation are entirely unpredictable.
Examples of code with undefined behavior are a = a++;, a[n]
= b[n++] and a[i++] = i;. Some more complicated cases are not
diagnosed by this option, and it may give an occasional false positive
result, but in general it has been found fairly effective at detecting
this sort of problem in programs.
The present implementation of this option only works for C programs. A future implementation may also work for C++ programs.
There is some controversy over the precise meaning of the sequence point rules in subtle cases. Links to papers with alternative formal definitions and other related discussions may be found on our readings page http://gcc.gnu.org/readings.html.
-Wreturn-type
int. Also warn about any return statement with no
return-value in a function whose return-type is not void.
For C++, a function without return type always produces a diagnostic message, even when `-Wno-return-type' is specified. The only exceptions are `main' and functions defined in system headers.
-Wswitch
switch statement has an index of enumeral type
and lacks a case for one or more of the named codes of that
enumeration. (The presence of a default label prevents this
warning.) case labels outside the enumeration range also
provoke warnings when this option is used.
-Wtrigraphs
-Wunused-function
-Wunused-label
To suppress this warning use the `unused' attribute (see section 5.33 Specifying Attributes of Variables).
-Wunused-parameter
To suppress this warning use the `unused' attribute (see section 5.33 Specifying Attributes of Variables).
-Wunused-variable
To suppress this warning use the `unused' attribute (see section 5.33 Specifying Attributes of Variables).
-Wunused-value
To suppress this warning cast the expression to `void'.
-Wunused
In order to get a warning about an unused function parameter, you must either specify `-W -Wunused' or separately specify `-Wunused-parameter'.
-Wuninitialized
setjmp call.
These warnings are possible only in optimizing compilation, because they require data flow information that is computed only when optimizing. If you don't specify `-O', you simply won't get these warnings.
These warnings occur only for variables that are candidates for
register allocation. Therefore, they do not occur for a variable that
is declared volatile, or whose address is taken, or whose size
is other than 1, 2, 4 or 8 bytes. Also, they do not occur for
structures, unions or arrays, even when they are in registers.
Note that there may be no warning about a variable that is used only to compute a value that itself is never used, because such computations may be deleted by data flow analysis before the warnings are printed.
These warnings are made optional because GCC is not smart enough to see all the reasons why the code might be correct despite appearing to have an error. Here is one example of how this can happen:
{
int x;
switch (y)
{
case 1: x = 1;
break;
case 2: x = 4;
break;
case 3: x = 5;
}
foo (x);
}
|
If the value of y is always 1, 2 or 3, then x is
always initialized, but GCC doesn't know this. Here is
another common case:
{
int save_y;
if (change_y) save_y = y, y = new_y;
...
if (change_y) y = save_y;
}
|
This has no bug because save_y is used only if it is set.
This option also warns when a non-volatile automatic variable might be
changed by a call to longjmp. These warnings as well are possible
only in optimizing compilation.
The compiler sees only the calls to setjmp. It cannot know
where longjmp will be called; in fact, a signal handler could
call it at any point in the code. As a result, you may get a warning
even when there is in fact no problem because longjmp cannot
in fact be called at the place which would cause a problem.
Some spurious warnings can be avoided if you declare all the functions
you use that never return as noreturn. See section 5.26 Declaring Attributes of Functions.
-Wreorder (C++ only)
-Wunknown-pragmas
-Wall
-Wsystem-headers
The following `-W...' options are not implied by `-Wall'. Some of them warn about constructions that users generally do not consider questionable, but which occasionally you might wish to check for; others warn about constructions that are necessary or hard to avoid in some cases, and there is no simple way to modify the code to suppress the warning.
-W
foo (a)
{
if (a > 0)
return a;
}
|
static are not the first things in
a declaration. According to the C Standard, this usage is obsolescent.
const.
Such a type qualifier has no effect, since the value returned by a
function is not an lvalue. (But don't warn about the GNU extension of
volatile void return types. That extension will be warned about
if `-pedantic' is specified.)
x.h:
struct s { int f, g; };
struct t { struct s h; int i; };
struct t x = { 1, 2, 3 };
|
x.h would be implicitly initialized to zero:
struct s { int f, g, h; };
struct s x = { 3, 4 };
|
-Wfloat-equal
The idea behind this is that sometimes it is convenient (for the programmer) to consider floating-point values as approximations to infinitely precise real numbers. If you are doing this, then you need to compute (by analysing the code, or in some other way) the maximum or likely maximum error that the computation introduces, and allow for it when performing comparisons (and when producing output, but that's a different problem). In particular, instead of testing for equality, you would check to see whether the two values have ranges that overlap; and this is done with the relational operators, so equality comparisons are probably mistaken.
-Wtraditional (C only)
<limits.h>.
Use of these macros in user code might normally lead to spurious
warnings, however gcc's integrated preprocessor has enough context to
avoid warning in these cases.
switch statement has an operand of type long.
static function declaration follows a static one.
This construct is not accepted by some traditional C compilers.
__STDC__ to avoid missing
initializer warnings and relies on default initialization to zero in the
traditional C case.
-Wundef
-Wshadow
-Wid-clash-len
-Wlarger-than-len
-Wpointer-arith
void. GNU C assigns these types a size of 1, for
convenience in calculations with void * pointers and pointers
to functions.
-Wbad-function-cast (C only)
int malloc() is cast to anything *.
-Wcast-qual
const char * is cast
to an ordinary char *.
-Wcast-align
char * is cast to
an int * on machines where integers can only be accessed at
two- or four-byte boundaries.
-Wwrite-strings
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 constants to char *.
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.
-Wconversion
Also, warn if a negative integer constant expression is implicitly
converted to an unsigned type. For example, warn about the assignment
x = -1 if x is unsigned. But do not warn about explicit
casts like (unsigned) -1.
-Wsign-compare
-Waggregate-return
-Wstrict-prototypes (C only)
-Wmissing-prototypes (C only)
-Wmissing-declarations
-Wmissing-noreturn
noreturn.
Note these are only possible candidates, not absolute ones. Care should
be taken to manually verify functions actually do not ever return before
adding the noreturn attribute, otherwise subtle code generation
bugs could be introduced. You will not get a warning for main in
hosted C environments.
-Wmissing-format-attribute
format attributes. Note these are only possible
candidates, not absolute ones. GCC will guess that format
attributes might be appropriate for any function that calls a function
like vprintf or vscanf, but this might not always be the
case, and some functions for which format attributes are
appropriate may not be detected. This option has no effect unless
`-Wformat' is enabled (possibly by `-Wall').
-Wpacked
f.x in struct bar
will be misaligned even though struct bar does not itself
have the packed attribute:
struct foo {
int x;
char a, b, c, d;
} __attribute__((packed));
struct bar {
char z;
struct foo f;
};
|
-Wpadded
-Wredundant-decls
-Wnested-externs (C only)
extern declaration is encountered within a function.
-Wunreachable-code
This option is intended to warn when the compiler detects that at least a whole line of source code will never be executed, because some condition is never satisfied or because it is after a procedure that never returns.
It is possible for this option to produce a warning even though there are circumstances under which part of the affected line can be executed, so care should be taken when removing apparently-unreachable code.
For instance, when a function is inlined, a warning may mean that the line is unreachable in only one inlined copy of the function.
This option is not made part of `-Wall' because in a debugging version of a program there is often substantial code which checks correct functioning of the program and is, hopefully, unreachable because the program does work. Another common use of unreachable code is to provide behaviour which is selectable at compile-time.
-Winline
-Wlong-long
-Wdisabled-optimization
-Werror
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
GCC has various special options that are used for debugging either your program or GCC:
-g
On most systems that use stabs format, `-g' enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but will probably make other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use `-gstabs+', `-gstabs', `-gxcoff+', `-gxcoff', `-gdwarf-1+', or `-gdwarf-1' (see below).
Unlike most other C compilers, GCC allows you to use `-g' with `-O'. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.
Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.
The following options are useful when GCC is generated with the capability for more than one debugging format.
-ggdb
-gstabs
-gstabs+
-gcoff
-gxcoff
-gxcoff+
-gdwarf
-gdwarf+
-gdwarf-2
-glevel
-ggdblevel
-gstabslevel
-gcofflevel
-gxcofflevel
-gdwarflevel
-gdwarf-2level
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, but no information about local variables and no line numbers.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use `-g3'.
-p
prof. You must use this option when compiling
the source files you want data about, and you must also use it when
linking.
-pg
gprof. You must use this option when compiling
the source files you want data about, and you must also use it when
linking.
-a
This data could be analyzed by a program like tcov. Note,
however, that the format of the data is not what tcov expects.
Eventually GNU gprof should be extended to process this data.
-Q
-ftime-report