Node: C++ Dialect Options, Next: Objective-C Dialect Options, Previous: C Dialect Options, Up: Invoking GCC
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
The default is version 1.
-fno-access-control
-fcheck-new
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
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
$
in identifiers. You can also explicitly prohibit use of
$
with the option -fno-dollars-in-identifiers
. (GNU C allows
$
by default on most target systems, but there are a few exceptions.)
Traditional C allowed the character $
to form part of
identifiers. However, ISO C and C++ forbid $
in identifiers.
-fno-elide-constructors
-fno-enforce-eh-specs
NDEBUG
. The compiler
will still optimize based on the exception specifications.
-fexternal-templates
Cause #pragma interface
and implementation
to apply to
template instantiation; template instances are emitted or not according
to the location of the template definition. See Template Instantiation, for more information.
This option is deprecated.
-falt-external-templates
-fexternal-templates
, but template instances are
emitted or not according to the place where they are first instantiated.
See Template Instantiation, for more information.
This option is deprecated.
-ffor-scope
-fno-for-scope
-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
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
#pragma implementation
. This will cause linker
errors if these functions are not inlined everywhere they are called.
-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
-fpermissive
will allow some
nonconforming code to compile.
-frepo
-fno-implicit-templates
. See Template Instantiation, for more information.
-fno-rtti
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.
-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
.
-fvtable-gc
-ffunction-sections
and -Wl,--gc-sections
, in order to
also discard the functions themselves.
This optimization requires GNU as and GNU ld. Not all systems support
this option. -Wl,--gc-sections
is ignored without -static
.
-fno-weak
-nostdinc++
In addition, these optimization, warning, and code generation options have meanings only for C++ programs:
-fno-default-inline
inline
for functions defined inside a class scope.
See Options That Control Optimization. Note that these
functions will have linkage like inline functions; they just won't be
inlined by default.
-Wabi
(C++ only)
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:
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
asA::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.
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.
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
.
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.
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.
-Wctor-dtor-privacy
(C++ only)
-Wnon-virtual-dtor
(C++ only)
-Wall
.
-Wreorder
(C++ only)
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++ only)
operator=
return a reference to *this
.
Also warn about violations of the following style guidelines from Scott Meyers' More Effective C++ book:
&&
, ||
, 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.
-Wno-deprecated
(C++ only)
-Wno-non-template-friend
(C++ only)
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++ only)
static_cast
,
reinterpret_cast
, and const_cast
) are less vulnerable to
unintended effects and much easier to search for.
-Woverloaded-virtual
(C++ only)
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++ 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 =
.