Options Controlling C Dialect
The following options control the dialect of C (or languages derived
from C, such as C++, Objective-C and Objective-C++) that the compiler
accepts:
-ansi
In C mode, this is equivalent to -std=c89. In C++ mode, it is
equivalent to -std=c++98.
This turns off certain features of GCC that are incompatible with
ISO C90 (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.
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 that would normally be built in but do not have semantics
defined by ISO C (such as "alloca" and "ffs") are not built-in
functions when -ansi is used.
-std=
Determine the language standard. This option is currently only
supported when compiling C or C++.
The compiler can accept several base standards, such as c89 or
c++98, and GNU dialects of those standards, such as gnu89 or
gnu++98. By specifying a base standard, the compiler will accept
all programs following that standard and those using GNU extensions
that do not contradict it. For example, -std=c89 turns off certain
features of GCC that are incompatible with ISO C90, such as the
"asm" and "typeof" keywords, but not other GNU extensions that do
not have a meaning in ISO C90, such as omitting the middle term of
a "?:" expression. On the other hand, by specifying a GNU dialect
of a standard, all features the compiler support are enabled, even
when those features change the meaning of the base standard and
some strict-conforming programs may be rejected. The particular
standard is used by -pedantic to identify which features are GNU
extensions given that version of the standard. For example
-std=gnu89 -pedantic would warn about C++ style // comments, while
-std=gnu99 -pedantic would not.
A value for this option must be provided; possible values are
c89
iso9899:1990
Support all ISO C90 programs (certain GNU extensions that
conflict with ISO C90 are disabled). Same as -ansi for C code.
iso9899:199409
ISO C90 as modified in amendment 1.
c99
c9x
iso9899:1999
iso9899:199x
ISO C99. Note that this standard is not yet fully supported;
see <http://gcc.gnu.org/gcc-4.4/c99status.html> for more
information. The names c9x and iso9899:199x are deprecated.
gnu89
GNU dialect of ISO C90 (including some C99 features). This is
the default for C code.
gnu99
gnu9x
GNU dialect of ISO C99. When ISO C99 is fully implemented in
GCC, this will become the default. The name gnu9x is
deprecated.
c++98
The 1998 ISO C++ standard plus amendments. Same as -ansi for
C++ code.
gnu++98
GNU dialect of -std=c++98. This is the default for C++ code.
c++0x
The working draft of the upcoming ISO C++0x standard. This
option enables experimental features that are likely to be
included in C++0x. The working draft is constantly changing,
and any feature that is enabled by this flag may be removed
from future versions of GCC if it is not part of the C++0x
standard.
gnu++0x
GNU dialect of -std=c++0x. This option enables experimental
features that may be removed in future versions of GCC.
-fgnu89-inline
The option -fgnu89-inline tells GCC to use the traditional GNU
semantics for "inline" functions when in C99 mode.
This option is accepted and ignored by GCC versions 4.1.3 up to
but not including 4.3. In GCC versions 4.3 and later it changes
the behavior of GCC in C99 mode. Using this option is roughly
equivalent to adding the "gnu_inline" function attribute to all
inline functions.
The option -fno-gnu89-inline explicitly tells GCC to use the C99
semantics for "inline" when in C99 or gnu99 mode (i.e., it
specifies the default behavior). This option was first supported
in GCC 4.3. This option is not supported in C89 or gnu89 mode.
The preprocessor macros "__GNUC_GNU_INLINE__" and
"__GNUC_STDC_INLINE__" may be used to check which semantics are in
effect for "inline" functions.
-aux-info filename
Output to the given filename prototyped declarations for all
functions declared and/or defined in a translation unit, including
those in header files. This option is silently ignored in any
language other than C.
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
Do not recognize "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
-fno-builtin-function
Don't recognize built-in functions that do not begin with
__builtin_ as prefix.
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 addition, when a function is recognized as a
built-in function, GCC may use information about that function to
warn about problems with calls to that function, or to generate
more efficient code, even if the resulting code still contains
calls to that function. For example, warnings are given with
-Wformat for bad calls to "printf", when "printf" is built in, and
"strlen" is known not to modify global memory.
With the -fno-builtin-function option only the built-in function
function is disabled. function must not begin with __builtin_. If
a function is named that is not built-in in this version of GCC,
this option is ignored. There is no corresponding
-fbuiltin-function option; if you wish to enable built-in functions
selectively when using -fno-builtin or -ffreestanding, you may
define macros such as:
#define abs(n) __builtin_abs ((n))
#define strcpy(d, s) __builtin_strcpy ((d), (s))
-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.
-fopenmp
Enable handling of OpenMP directives "#pragma omp" in C/C++ and
"!$omp" in Fortran. When -fopenmp is specified, the compiler
generates parallel code according to the OpenMP Application Program
Interface v2.5 <http://www.openmp.org/>. This option implies
-pthread, and thus is only supported on targets that have support
for -pthread.
-fms-extensions
Accept some non-standard constructs used in Microsoft header files.
Some cases of unnamed fields in structures and unions are only
accepted with this option.
-trigraphs
Support ISO C trigraphs. The -ansi option (and -std options for
strict ISO C conformance) implies -trigraphs.
-no-integrated-cpp
Performs a compilation in two passes: preprocessing and compiling.
This option allows a user supplied "cc1", "cc1plus", or "cc1obj"
via the -B option. The user supplied compilation step can then add
in an additional preprocessing step after normal preprocessing but
before compiling. The default is to use the integrated cpp
(internal cpp)
The semantics of this option will change if "cc1", "cc1plus", and
"cc1obj" are merged.
-traditional
-traditional-cpp
Formerly, these options caused GCC to attempt to emulate a pre-
standard C compiler. They are now only supported with the -E
switch. The preprocessor continues to support a pre-standard mode.
See the GNU CPP manual for details.
-fcond-mismatch
Allow conditional expressions with mismatched types in the second
and third arguments. The value of such an expression is void.
This option is not supported for C++.
-flax-vector-conversions
Allow implicit conversions between vectors with differing numbers
of elements and/or incompatible element types. This option should
not be used for new code.
-funsigned-char
Let the type "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
Let the type "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
These options control whether a bit-field is signed or unsigned,
when the declaration does not use either "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.
Options Controlling C++ Dialect
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:
-fabi-version=n
Use version n of the C++ ABI. Version 2 is the version of the C++
ABI that first appeared in G++ 3.4. Version 1 is the version of
the C++ ABI that first appeared in G++ 3.2. Version 0 will always
be the version that conforms most closely to the C++ ABI
specification. Therefore, the ABI obtained using version 0 will
change as ABI bugs are fixed.
The default is version 2.
-fno-access-control
Turn off all access checking. This switch is mainly useful for
working around bugs in the access control code.
-fcheck-new
Check that the pointer returned by "operator new" is non-null
before attempting to modify the storage allocated. This check is
normally unnecessary because the C++ standard specifies that
"operator new" will only return 0 if it is declared throw(), in
which case the compiler will always check the return value even
without this option. In all other cases, when "operator new" has a
non-empty exception specification, memory exhaustion is signalled
by throwing "std::bad_alloc". See also new (nothrow).
-fconserve-space
Put uninitialized or runtime-initialized global variables into the
common segment, as C does. This saves space in the executable at
the cost of not diagnosing duplicate definitions. If you compile
with this flag and your program mysteriously crashes after "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-deduce-init-list
Disable deduction of a template type parameter as
std::initializer_list from a brace-enclosed initializer list, i.e.
template <class T> auto forward(T t) -> decltype (realfn (t))
{
return realfn (t);
}
void f()
{
forward({1,2}); // call forward<std::initializer_list<int>>
}
This option is present because this deduction is an extension to
the current specification in the C++0x working draft, and there was
some concern about potential overload resolution problems.
-ffriend-injection
Inject friend functions into the enclosing namespace, so that they
are visible outside the scope of the class in which they are
declared. Friend functions were documented to work this way in the
old Annotated C++ Reference Manual, and versions of G++ before 4.1
always worked that way. However, in ISO C++ a friend function
which is not declared in an enclosing scope can only be found using
argument dependent lookup. This option causes friends to be
injected as they were in earlier releases.
This option is for compatibility, and may be removed in a future
release of G++.
-fno-elide-constructors
The C++ standard allows an implementation to omit creating a
temporary which is only used to initialize another object of the
same type. Specifying this option disables that optimization, and
forces G++ to call the copy constructor in all cases.
-fno-enforce-eh-specs
Don't generate code to check for violation of exception
specifications at runtime. This option violates the C++ standard,
but may be useful for reducing code size in production builds, much
like defining NDEBUG. This does not give user code permission to
throw exceptions in violation of the exception specifications; the
compiler will still optimize based on the specifications, so
throwing an unexpected exception will result in undefined behavior.
-ffor-scope
-fno-for-scope
If -ffor-scope is specified, the scope of variables declared in a
for-init-statement is limited to the for loop itself, as specified
by the C++ standard. If -fno-for-scope is specified, the scope of
variables declared in a for-init-statement extends to the end of
the enclosing scope, as was the case in old versions of G++, and
other (traditional) implementations of C++.
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
Do not recognize "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
Never emit code for non-inline templates which are instantiated
implicitly (i.e. by use); only emit code for explicit
instantiations.
-fno-implicit-inline-templates
Don't emit code for implicit instantiations of inline templates,
either. The default is to handle inlines differently so that
compiles with and without optimization will need the same set of
explicit instantiations.
-fno-implement-inlines
To save space, do not emit out-of-line copies of inline functions
controlled by #pragma implementation. This will cause linker
errors if these functions are not inlined everywhere they are
called.
-fms-extensions
Disable pedantic warnings about constructs used in MFC, such as
implicit int and getting a pointer to member function via non-
standard syntax.
-fno-nonansi-builtins
Disable built-in declarations of functions that are not mandated by
ANSI/ISO C. These include "ffs", "alloca", "_exit", "index",
"bzero", "conjf", and other related functions.
-fno-operator-names
Do not treat the operator name keywords "and", "bitand", "bitor",
"compl", "not", "or" and "xor" as synonyms as keywords.
-fno-optional-diags
Disable diagnostics that the standard says a compiler does not need
to issue. Currently, the only such diagnostic issued by G++ is the
one for a name having multiple meanings within a class.
-fpermissive
Downgrade some diagnostics about nonconformant code from errors to
warnings. Thus, using -fpermissive will allow some nonconforming
code to compile.
-frepo
Enable automatic template instantiation at link time. This option
also implies -fno-implicit-templates.
-fno-rtti
Disable generation of information about every class with virtual
functions for use by the C++ runtime type identification features
(dynamic_cast and typeid). If you don't use those parts of the
language, you can save some space by using this flag. Note that
exception handling uses the same information, but it will generate
it as needed. The dynamic_cast operator can still be used for casts
that do not require runtime type information, i.e. casts to "void
*" or to unambiguous base classes.
-fstats
Emit statistics about front-end processing at the end of the
compilation. This information is generally only useful to the G++
development team.
-ftemplate-depth-n
Set the maximum instantiation depth for template classes to n. A
limit on the template instantiation depth is needed to detect
endless recursions during template class instantiation. ANSI/ISO
C++ conforming programs must not rely on a maximum depth greater
than 17.
-fno-threadsafe-statics
Do not emit the extra code to use the routines specified in the C++
ABI for thread-safe initialization of local statics. You can use
this option to reduce code size slightly in code that doesn't need
to be thread-safe.
-fuse-cxa-atexit
Register destructors for objects with static storage duration with
the "__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-use-cxa-get-exception-ptr
Don't use the "__cxa_get_exception_ptr" runtime routine. This will
cause "std::uncaught_exception" to be incorrect, but is necessary
if the runtime routine is not available.
-fvisibility-inlines-hidden
This switch declares that the user does not attempt to compare
pointers to inline methods where the addresses of the two functions
were taken in different shared objects.
The effect of this is that GCC may, effectively, mark inline
methods with "__attribute__ ((visibility ("hidden")))" so that they
do not appear in the export table of a DSO and do not require a PLT
indirection when used within the DSO. Enabling this option can
have a dramatic effect on load and link times of a DSO as it
massively reduces the size of the dynamic export table when the
library makes heavy use of templates.
The behavior of this switch is not quite the same as marking the
methods as hidden directly, because it does not affect static
variables local to the function or cause the compiler to deduce
that the function is defined in only one shared object.
You may mark a method as having a visibility explicitly to negate
the effect of the switch for that method. For example, if you do
want to compare pointers to a particular inline method, you might
mark it as having default visibility. Marking the enclosing class
with explicit visibility will have no effect.
Explicitly instantiated inline methods are unaffected by this
option as their linkage might otherwise cross a shared library
boundary.
-fvisibility-ms-compat
This flag attempts to use visibility settings to make GCC's C++
linkage model compatible with that of Microsoft Visual Studio.
The flag makes these changes to GCC's linkage model:
1. It sets the default visibility to "hidden", like
-fvisibility=hidden.
2. Types, but not their members, are not hidden by default.
3. The One Definition Rule is relaxed for types without explicit
visibility specifications which are defined in more than one
different shared object: those declarations are permitted if
they would have been permitted when this option was not used.
In new code it is better to use -fvisibility=hidden and export
those classes which are intended to be externally visible.
Unfortunately it is possible for code to rely, perhaps
accidentally, on the Visual Studio behavior.
Among the consequences of these changes are that static data
members of the same type with the same name but defined in
different shared objects will be different, so changing one will
not change the other; and that pointers to function members defined
in different shared objects may not compare equal. When this flag
is given, it is a violation of the ODR to define types with the
same name differently.
-fno-weak
Do not use weak symbol support, even if it is provided by the
linker. By default, G++ will use weak symbols if they are
available. This option exists only for testing, and should not be
used by end-users; it will result in inferior code and has no
benefits. This option may be removed in a future release of G++.
-nostdinc++
Do not search for header files in the standard directories specific
to C++, but do still search the other standard directories. (This
option is used when building the C++ library.)
In addition, these optimization, warning, and code generation options
have meanings only for C++ programs:
-fno-default-inline
Do not assume inline for functions defined inside a class scope.
Note that these functions will have linkage like inline
functions; they just won't be inlined by default.
-Wabi (C, Objective-C, C++ and Objective-C++ only)
Warn when G++ generates code that is probably not compatible with
the vendor-neutral C++ ABI. Although an effort has been made to
warn about all such cases, there are probably some cases that are
not warned about, even though G++ is generating incompatible code.
There may also be cases where warnings are emitted even though the
code that is generated will be compatible.
You should rewrite your code to avoid these warnings if you are
concerned about the fact that code generated by G++ may not be
binary compatible with code generated by other compilers.
The known incompatibilities at this point include:
· Incorrect handling of tail-padding for bit-fields. G++ may
attempt to pack data into the same byte as a base class. For
example:
struct A { virtual void f(); int f1 : 1; };
struct B : public A { int f2 : 1; };
In this case, G++ will place "B::f2" into the same byte
as"A::f1"; other compilers will not. You can avoid this
problem by explicitly padding "A" so that its size is a
multiple of the byte size on your platform; that will cause G++
and other compilers to layout "B" identically.
· Incorrect handling of tail-padding for virtual bases. G++ does
not use tail padding when laying out virtual bases. For
example:
struct A { virtual void f(); char c1; };
struct B { B(); char c2; };
struct C : public A, public virtual B {};
In this case, G++ will not place "B" into the tail-padding for
"A"; other compilers will. You can avoid this problem by
explicitly padding "A" so that its size is a multiple of its
alignment (ignoring virtual base classes); that will cause G++
and other compilers to layout "C" identically.
· Incorrect handling of bit-fields with declared widths greater
than that of their underlying types, when the bit-fields appear
in a union. For example:
union U { int i : 4096; };
Assuming that an "int" does not have 4096 bits, G++ will make
the union too small by the number of bits in an "int".
· Empty classes can be placed at incorrect offsets. For example:
struct A {};
struct B {
A a;
virtual void f ();
};
struct C : public B, public A {};
G++ will place the "A" base class of "C" at a nonzero offset;
it should be placed at offset zero. G++ mistakenly believes
that the "A" data member of "B" is already at offset zero.
· Names of template functions whose types involve "typename" or
template template parameters can be mangled incorrectly.
template <typename Q>
void f(typename Q::X) {}
template <template <typename> class Q>
void f(typename Q<int>::X) {}
Instantiations of these templates may be mangled incorrectly.
It also warns psABI related changes. The known psABI changes at
this point include:
· For SYSV/x86-64, when passing union with long double, it is
changed to pass in memory as specified in psABI. For example:
union U {
long double ld;
int i;
};
"union U" will always be passed in memory.
-Wctor-dtor-privacy (C++ and Objective-C++ only)
Warn when a class seems unusable because all the constructors or
destructors in that class are private, and it has neither friends
nor public static member functions.
-Wnon-virtual-dtor (C++ and Objective-C++ only)
Warn when a class has virtual functions and accessible non-virtual
destructor, in which case it would be possible but unsafe to delete
an instance of a derived class through a pointer to the base class.
This warning is also enabled if -Weffc++ is specified.
-Wreorder (C++ and Objective-C++ only)
Warn when the order of member initializers given in the code does
not match the order in which they must be executed. For instance:
struct A {
int i;
int j;
A(): j (0), i (1) { }
};
The compiler will rearrange the member initializers for i and j to
match the declaration order of the members, emitting a warning to
that effect. This warning is enabled by -Wall.
The following -W... options are not affected by -Wall.
-Weffc++ (C++ and Objective-C++ only)
Warn about violations of the following style guidelines from Scott
Meyers' Effective C++ book:
· Item 11: Define a copy constructor and an assignment operator
for classes with dynamically allocated memory.
· Item 12: Prefer initialization to assignment in constructors.
· Item 14: Make destructors virtual in base classes.
· Item 15: Have "operator=" return a reference to *this.
· Item 23: Don't try to return a reference when you must return
an object.
Also warn about violations of the following style guidelines from
Scott Meyers' More Effective C++ book:
· Item 6: Distinguish between prefix and postfix forms of
increment and decrement operators.
· Item 7: Never overload "&&", "||", or ",".
When selecting this option, be aware that the standard library
headers do not obey all of these guidelines; use grep -v to filter
out those warnings.
-Wstrict-null-sentinel (C++ and Objective-C++ only)
Warn also about the use of an uncasted "NULL" as sentinel. When
compiling only with GCC this is a valid sentinel, as "NULL" is
defined to "__null". Although it is a null pointer constant not a
null pointer, it is guaranteed to be of the same size as a pointer.
But this use is not portable across different compilers.
-Wno-non-template-friend (C++ and Objective-C++ only)
Disable warnings when non-templatized friend functions are declared
within a template. Since the advent of explicit template
specification support in G++, if the name of the friend is an
unqualified-id (i.e., friend foo(int)), the C++ language
specification demands that the friend declare or define an
ordinary, nontemplate function. (Section 14.5.3). Before G++
implemented explicit specification, unqualified-ids could be
interpreted as a particular specialization of a templatized
function. Because this non-conforming behavior is no longer the
default behavior for G++, -Wnon-template-friend allows the compiler
to check existing code for potential trouble spots and is on by
default. This new compiler behavior can be turned off with
-Wno-non-template-friend which keeps the conformant compiler code
but disables the helpful warning.
-Wold-style-cast (C++ and Objective-C++ only)
Warn if an old-style (C-style) cast to a non-void type is used
within a C++ program. The new-style casts (dynamic_cast,
static_cast, reinterpret_cast, and const_cast) are less vulnerable
to unintended effects and much easier to search for.
-Woverloaded-virtual (C++ and Objective-C++ only)
Warn when a function declaration hides virtual functions from a
base class. For example, in:
struct A {
virtual void f();
};
struct B: public A {
void f(int);
};
the "A" class version of "f" is hidden in "B", and code like:
B* b;
b->f();
will fail to compile.
-Wno-pmf-conversions (C++ and Objective-C++ only)
Disable the diagnostic for converting a bound pointer to member
function to a plain pointer.
-Wsign-promo (C++ and Objective-C++ only)
Warn when overload resolution chooses a promotion from unsigned or
enumerated type to a signed type, over a conversion to an unsigned
type of the same size. Previous versions of G++ would try to
preserve unsignedness, but the standard mandates the current
behavior.
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 =.
Options Controlling Objective-C and Objective-C++ Dialects
(NOTE: This manual does not describe the Objective-C and Objective-C++
languages themselves. See
This section describes the command-line options that are only
meaningful for Objective-C and Objective-C++ programs, but you can also
use most of the language-independent GNU compiler options. For
example, you might compile a file "some_class.m" like this:
gcc -g -fgnu-runtime -O -c some_class.m
In this example, -fgnu-runtime is an option meant only for Objective-C
and Objective-C++ programs; you can use the other options with any
language supported by GCC.
Note that since Objective-C is an extension of the C language,
Objective-C compilations may also use options specific to the C front-
end (e.g., -Wtraditional). Similarly, Objective-C++ compilations may
use C++-specific options (e.g., -Wabi).
Here is a list of options that are only for compiling Objective-C and
Objective-C++ programs:
-fconstant-string-class=class-name
Use class-name as the name of the class to instantiate for each
literal string specified with the syntax "@"..."". The default
class name is "NXConstantString" if the GNU runtime is being used,
and "NSConstantString" if the NeXT runtime is being used (see
below). The -fconstant-cfstrings option, if also present, will
override the -fconstant-string-class setting and cause "@"...""
literals to be laid out as constant CoreFoundation strings.
-fgnu-runtime
Generate object code compatible with the standard GNU Objective-C
runtime. This is the default for most types of systems.
-fnext-runtime
Generate output compatible with the NeXT runtime. This is the
default for NeXT-based systems, including Darwin and Mac OS X. The
macro "__NEXT_RUNTIME__" is predefined if (and only if) this option
is used.
-fno-nil-receivers
Assume that all Objective-C message dispatches (e.g., "[receiver
message:arg]") in this translation unit ensure that the receiver is
not "nil". This allows for more efficient entry points in the
runtime to be used. Currently, this option is only available in
conjunction with the NeXT runtime on Mac OS X 10.3 and later.
-fobjc-call-cxx-cdtors
For each Objective-C class, check if any of its instance variables
is a C++ object with a non-trivial default constructor. If so,
synthesize a special "- (id) .cxx_construct" instance method that
will run non-trivial default constructors on any such instance
variables, in order, and then return "self". Similarly, check if
any instance variable is a C++ object with a non-trivial
destructor, and if so, synthesize a special "- (void)
.cxx_destruct" method that will run all such default destructors,
in reverse order.
The "- (id) .cxx_construct" and/or "- (void) .cxx_destruct" methods
thusly generated will only operate on instance variables declared
in the current Objective-C class, and not those inherited from
superclasses. It is the responsibility of the Objective-C runtime
to invoke all such methods in an object's inheritance hierarchy.
The "- (id) .cxx_construct" methods will be invoked by the runtime
immediately after a new object instance is allocated; the "- (void)
.cxx_destruct" methods will be invoked immediately before the
runtime deallocates an object instance.
As of this writing, only the NeXT runtime on Mac OS X 10.4 and
later has support for invoking the "- (id) .cxx_construct" and "-
(void) .cxx_destruct" methods.
-fobjc-direct-dispatch
Allow fast jumps to the message dispatcher. On Darwin this is
accomplished via the comm page.
-fobjc-exceptions
Enable syntactic support for structured exception handling in
Objective-C, similar to what is offered by C++ and Java. This
option is unavailable in conjunction with the NeXT runtime on Mac
OS X 10.2 and earlier.
@try {
...
@throw expr;
...
}
@catch (AnObjCClass *exc) {
...
@throw expr;
...
@throw;
...
}
@catch (AnotherClass *exc) {
...
}
@catch (id allOthers) {
...
}
@finally {
...
@throw expr;
...
}
The @throw statement may appear anywhere in an Objective-C or
Objective-C++ program; when used inside of a @catch block, the
@throw may appear without an argument (as shown above), in which
case the object caught by the @catch will be rethrown.
Note that only (pointers to) Objective-C objects may be thrown and
caught using this scheme. When an object is thrown, it will be
caught by the nearest @catch clause capable of handling objects of
that type, analogously to how "catch" blocks work in C++ and Java.
A "@catch(id ...)" clause (as shown above) may also be provided to
catch any and all Objective-C exceptions not caught by previous
@catch clauses (if any).
The @finally clause, if present, will be executed upon exit from
the immediately preceding "@try ... @catch" section. This will
happen regardless of whether any exceptions are thrown, caught or
rethrown inside the "@try ... @catch" section, analogously to the
behavior of the "finally" clause in Java.
There are several caveats to using the new exception mechanism:
· Although currently designed to be binary compatible with
"NS_HANDLER"-style idioms provided by the "NSException" class,
the new exceptions can only be used on Mac OS X 10.3 (Panther)
and later systems, due to additional functionality needed in
the (NeXT) Objective-C runtime.
· As mentioned above, the new exceptions do not support handling
types other than Objective-C objects. Furthermore, when used
from Objective-C++, the Objective-C exception model does not
interoperate with C++ exceptions at this time. This means you
cannot @throw an exception from Objective-C and "catch" it in
C++, or vice versa (i.e., "throw ... @catch").
The -fobjc-exceptions switch also enables the use of
synchronization blocks for thread-safe execution:
@synchronized (ObjCClass *guard) {
...
}
Upon entering the @synchronized block, a thread of execution shall
first check whether a lock has been placed on the corresponding
"guard" object by another thread. If it has, the current thread
shall wait until the other thread relinquishes its lock. Once
"guard" becomes available, the current thread will place its own
lock on it, execute the code contained in the @synchronized block,
and finally relinquish the lock (thereby making "guard" available
to other threads).
Unlike Java, Objective-C does not allow for entire methods to be
marked @synchronized. Note that throwing exceptions out of
@synchronized blocks is allowed, and will cause the guarding object
to be unlocked properly.
-fobjc-gc
Enable garbage collection (GC) in Objective-C and Objective-C++
programs.
-freplace-objc-classes
Emit a special marker instructing ld(1) not to statically link in
the resulting object file, and allow dyld(1) to load it in at run
time instead. This is used in conjunction with the Fix-and-
Continue debugging mode, where the object file in question may be
recompiled and dynamically reloaded in the course of program
execution, without the need to restart the program itself.
Currently, Fix-and-Continue functionality is only available in
conjunction with the NeXT runtime on Mac OS X 10.3 and later.
-fzero-link
When compiling for the NeXT runtime, the compiler ordinarily
replaces calls to "objc_getClass("...")" (when the name of the
class is known at compile time) with static class references that
get initialized at load time, which improves run-time performance.
Specifying the -fzero-link flag suppresses this behavior and causes
calls to "objc_getClass("...")" to be retained. This is useful in
Zero-Link debugging mode, since it allows for individual class
implementations to be modified during program execution.
-gen-decls
Dump interface declarations for all classes seen in the source file
to a file named sourcename.decl.
-Wassign-intercept (Objective-C and Objective-C++ only)
Warn whenever an Objective-C assignment is being intercepted by the
garbage collector.
-Wno-protocol (Objective-C and Objective-C++ only)
If a class is declared to implement a protocol, a warning is issued
for every method in the protocol that is not implemented by the
class. The default behavior is to issue a warning for every method
not explicitly implemented in the class, even if a method
implementation is inherited from the superclass. If you use the
-Wno-protocol option, then methods inherited from the superclass
are considered to be implemented, and no warning is issued for
them.
-Wselector (Objective-C and Objective-C++ only)
Warn if multiple methods of different types for the same selector
are found during compilation. The check is performed on the list
of methods in the final stage of compilation. Additionally, a
check is performed for each selector appearing in a
"@selector(...)" expression, and a corresponding method for that
selector has been found during compilation. Because these checks
scan the method table only at the end of compilation, these
warnings are not produced if the final stage of compilation is not
reached, for example because an error is found during compilation,
or because the -fsyntax-only option is being used.
-Wstrict-selector-match (Objective-C and Objective-C++ only)
Warn if multiple methods with differing argument and/or return
types are found for a given selector when attempting to send a
message using this selector to a receiver of type "id" or "Class".
When this flag is off (which is the default behavior), the compiler
will omit such warnings if any differences found are confined to
types which share the same size and alignment.
-Wundeclared-selector (Objective-C and Objective-C++ only)
Warn if a "@selector(...)" expression referring to an undeclared
selector is found. A selector is considered undeclared if no
method with that name has been declared before the "@selector(...)"
expression, either explicitly in an @interface or @protocol
declaration, or implicitly in an @implementation section. This
option always performs its checks as soon as a "@selector(...)"
expression is found, while -Wselector only performs its checks in
the final stage of compilation. This also enforces the coding
style convention that methods and selectors must be declared before
being used.
-print-objc-runtime-info
Generate C header describing the largest structure that is passed
by value, if any.
continue :
No comments:
Post a Comment