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
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
-fnext-runtime
__NEXT_RUNTIME__
is predefined if (and only if) this option is
used.
-fno-nil-receivers
(APPLE ONLY)[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.
-fobjc-direct-dispatch
(APPLE ONLY)objc_msgSend
) called very frequently by
Objective-C programs, special entry points exist in high memory that may be jumped
to directly (e.g., via the "bla"
instruction on the PowerPC) for improved
performance. The -fobjc-direct-dispatch option will cause such jumps
to be generated. This option is only available in conjunction with the NeXT
runtime; furthermore, programs built with the -fobjc-direct-dispatch
option will only run on Mac OS X 10.4 (Tiger) or later systems.
-fobjc-exceptions
(APPLE ONLY)MACOSX_DEPLOYMENT_TARGET
exists and is set
to "10.3"
or greater.
@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:
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.
@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:
ObjCClass *lockObject = ...; ... @synchronized (lockObject) { ... @throw expr; ... }
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.
-freplace-objc-classes
(APPLE ONLY)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
-freplace-objc-classes flag suppresses this behavior and causes calls
to objc_getClass("...")
to be retained. This is useful in
Fix-and-Continue debugging mode, since it allows for individual class
implementations to be modified during program execution.
-fzero-link
(APPLE ONLY)-gen-decls
-Wno-protocol
-Wno-protocol
option, then
methods inherited from the superclass are considered to be implemented,
and no warning is issued for them.
-Wselector
@selector(...)
expression, a corresponding method with 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.
-Wundeclared-selector
@selector(...)
expression referring to an
undeclared selector is found. A selector is considered undeclared if no
method with that name has been declared (explicitly, in an
@interface
or @protocol
declaration, or implicitly, in
an @implementation
section) before the
@selector(...)
expression. 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), and so additionally enforces the coding style convention
that methods and selectors must be declared before being used.