2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
6 .. index:: compiler options, Objective-C and Objective-C++, Objective-C and Objective-C++ options, command-line, options, Objective-C and Objective-C++
8 .. _objective-c-and-objective-c++-dialect-options:
10 Options Controlling Objective-C and Objective-C++ Dialects
11 **********************************************************
15 This manual does not describe the Objective-C and Objective-C++
16 languages themselves. See :ref:`standards`, for references.
18 This section describes the command-line options that are only meaningful
19 for Objective-C and Objective-C++ programs. You can also use most of
20 the language-independent GNU compiler options.
21 For example, you might compile a file :samp:`some_class.m` like this:
25 gcc -g -fgnu-runtime -O -c some_class.m
27 In this example, :option:`-fgnu-runtime` is an option meant only for
28 Objective-C and Objective-C++ programs; you can use the other options with
29 any language supported by GCC.
31 Note that since Objective-C is an extension of the C language, Objective-C
32 compilations may also use options specific to the C front-end (e.g.,
33 :option:`-Wtraditional`). Similarly, Objective-C++ compilations may use
34 C++-specific options (e.g., :option:`-Wabi`).
36 Here is a list of options that are *only* for compiling Objective-C
37 and Objective-C++ programs:
39 .. option:: -fconstant-string-class={class-name}
41 Use :samp:`{class-name}` as the name of the class to instantiate for each
42 literal string specified with the syntax ``@"..."``. The default
43 class name is ``NXConstantString`` if the GNU runtime is being used, and
44 ``NSConstantString`` if the NeXT runtime is being used (see below). The
45 :option:`-fconstant-cfstrings` option, if also present, overrides the
46 :option:`-fconstant-string-class` setting and cause ``@"..."`` literals
47 to be laid out as constant CoreFoundation strings.
49 .. option:: -fgnu-runtime
51 Generate object code compatible with the standard GNU Objective-C
52 runtime. This is the default for most types of systems.
54 .. option:: -fnext-runtime
56 Generate output compatible with the NeXT runtime. This is the default
57 for NeXT-based systems, including Darwin and Mac OS X. The macro
58 ``__NEXT_RUNTIME__`` is predefined if (and only if) this option is
61 .. option:: -fno-nil-receivers
63 Assume that all Objective-C message dispatches (``[receiver
64 message:arg]``) in this translation unit ensure that the receiver is
65 not ``nil``. This allows for more efficient entry points in the
66 runtime to be used. This option is only available in conjunction with
67 the NeXT runtime and ABI version 0 or 1.
69 .. option:: -fnil-receivers
71 Default setting; overrides :option:`-fno-nil-receivers`.
73 .. option:: -fobjc-abi-version={n}
75 Use version :samp:`{n}` of the Objective-C ABI for the selected runtime.
76 This option is currently supported only for the NeXT runtime. In that
77 case, Version 0 is the traditional (32-bit) ABI without support for
78 properties and other Objective-C 2.0 additions. Version 1 is the
79 traditional (32-bit) ABI with support for properties and other
80 Objective-C 2.0 additions. Version 2 is the modern (64-bit) ABI. If
81 nothing is specified, the default is Version 0 on 32-bit target
82 machines, and Version 2 on 64-bit target machines.
84 .. option:: -fobjc-call-cxx-cdtors
86 For each Objective-C class, check if any of its instance variables is a
87 C++ object with a non-trivial default constructor. If so, synthesize a
88 special ``- (id) .cxx_construct`` instance method which runs
89 non-trivial default constructors on any such instance variables, in order,
90 and then return ``self``. Similarly, check if any instance variable
91 is a C++ object with a non-trivial destructor, and if so, synthesize a
92 special ``- (void) .cxx_destruct`` method which runs
93 all such default destructors, in reverse order.
95 The ``- (id) .cxx_construct`` and ``- (void) .cxx_destruct``
96 methods thusly generated only operate on instance variables
97 declared in the current Objective-C class, and not those inherited
98 from superclasses. It is the responsibility of the Objective-C
99 runtime to invoke all such methods in an object's inheritance
100 hierarchy. The ``- (id) .cxx_construct`` methods are invoked
101 by the runtime immediately after a new object instance is allocated;
102 the ``- (void) .cxx_destruct`` methods are invoked immediately
103 before the runtime deallocates an object instance.
105 As of this writing, only the NeXT runtime on Mac OS X 10.4 and later has
106 support for invoking the ``- (id) .cxx_construct`` and
107 ``- (void) .cxx_destruct`` methods.
109 .. option:: -fobjc-direct-dispatch
111 Allow fast jumps to the message dispatcher. On Darwin this is
112 accomplished via the comm page.
114 .. option:: -fobjc-exceptions
116 Enable syntactic support for structured exception handling in
117 Objective-C, similar to what is offered by C++. This option
118 is required to use the Objective-C keywords ``@try``,
119 ``@throw``, ``@catch``, ``@finally`` and
120 ``@synchronized``. This option is available with both the GNU
121 runtime and the NeXT runtime (but not available in conjunction with
122 the NeXT runtime on Mac OS X 10.2 and earlier).
124 .. option:: -fobjc-gc
126 Enable garbage collection (GC) in Objective-C and Objective-C++
127 programs. This option is only available with the NeXT runtime; the
128 GNU runtime has a different garbage collection implementation that
129 does not require special compiler flags.
131 .. option:: -fobjc-nilcheck
133 For the NeXT runtime with version 2 of the ABI, check for a nil
134 receiver in method invocations before doing the actual method call.
135 This is the default and can be disabled using
136 :option:`-fno-objc-nilcheck`. Class methods and super calls are never
137 checked for nil in this way no matter what this flag is set to.
138 Currently this flag does nothing when the GNU runtime, or an older
139 version of the NeXT runtime ABI, is used.
141 .. option:: -fobjc-std=objc1
143 Conform to the language syntax of Objective-C 1.0, the language
144 recognized by GCC 4.0. This only affects the Objective-C additions to
145 the C/C++ language; it does not affect conformance to C/C++ standards,
146 which is controlled by the separate C/C++ dialect option flags. When
147 this option is used with the Objective-C or Objective-C++ compiler,
148 any Objective-C syntax that is not recognized by GCC 4.0 is rejected.
149 This is useful if you need to make sure that your Objective-C code can
150 be compiled with older versions of GCC.
152 .. option:: -freplace-objc-classes
154 Emit a special marker instructing :command:`ld(1)` not to statically link in
155 the resulting object file, and allow :command:`dyld(1)` to load it in at
156 run time instead. This is used in conjunction with the Fix-and-Continue
157 debugging mode, where the object file in question may be recompiled and
158 dynamically reloaded in the course of program execution, without the need
159 to restart the program itself. Currently, Fix-and-Continue functionality
160 is only available in conjunction with the NeXT runtime on Mac OS X 10.3
163 .. option:: -fzero-link
165 When compiling for the NeXT runtime, the compiler ordinarily replaces calls
166 to ``objc_getClass("...")`` (when the name of the class is known at
167 compile time) with static class references that get initialized at load time,
168 which improves run-time performance. Specifying the :option:`-fzero-link` flag
169 suppresses this behavior and causes calls to ``objc_getClass("...")``
170 to be retained. This is useful in Zero-Link debugging mode, since it allows
171 for individual class implementations to be modified during program execution.
172 The GNU runtime currently always retains calls to ``objc_get_class("...")``
173 regardless of command-line options.
175 .. option:: -fno-local-ivars
177 By default instance variables in Objective-C can be accessed as if
178 they were local variables from within the methods of the class they're
179 declared in. This can lead to shadowing between instance variables
180 and other variables declared either locally inside a class method or
181 globally with the same name. Specifying the :option:`-fno-local-ivars`
182 flag disables this behavior thus avoiding variable shadowing issues.
184 .. option:: -flocal-ivars
186 Default setting; overrides :option:`-fno-local-ivars`.
188 .. option:: -fivar-visibility=[public|protected|private|package]
190 Set the default instance variable visibility to the specified option
191 so that instance variables declared outside the scope of any access
192 modifier directives default to the specified visibility.
194 .. option:: -gen-decls
196 Dump interface declarations for all classes seen in the source file to a
197 file named :samp:`{sourcename}.decl`.
199 .. option:: -Wassign-intercept
203 Objective-C and Objective-C++ only
205 Warn whenever an Objective-C assignment is being intercepted by the
208 .. option:: -Wno-assign-intercept
210 Default setting; overrides :option:`-Wassign-intercept`.
212 .. option:: -Wno-property-assign-default
216 Objective-C and Objective-C++ only
218 Do not warn if a property for an Objective-C object has no assign
221 .. option:: -Wproperty-assign-default
223 Default setting; overrides :option:`-Wno-property-assign-default`.
225 .. option:: -Wno-protocol
229 Objective-C and Objective-C++ only
231 If a class is declared to implement a protocol, a warning is issued for
232 every method in the protocol that is not implemented by the class. The
233 default behavior is to issue a warning for every method not explicitly
234 implemented in the class, even if a method implementation is inherited
235 from the superclass. If you use the :option:`-Wno-protocol` option, then
236 methods inherited from the superclass are considered to be implemented,
237 and no warning is issued for them.
239 .. option:: -Wprotocol
241 Default setting; overrides :option:`-Wno-protocol`.
243 .. option:: -Wobjc-root-class
247 Objective-C and Objective-C++ only
249 Warn if a class interface lacks a superclass. Most classes will inherit
250 from ``NSObject`` (or ``Object``) for example. When declaring
251 classes intended to be root classes, the warning can be suppressed by
252 marking their interfaces with ``__attribute__((objc_root_class))``.
254 .. option:: -Wselector
258 Objective-C and Objective-C++ only
260 Warn if multiple methods of different types for the same selector are
261 found during compilation. The check is performed on the list of methods
262 in the final stage of compilation. Additionally, a check is performed
263 for each selector appearing in a ``@selector(...)``
264 expression, and a corresponding method for that selector has been found
265 during compilation. Because these checks scan the method table only at
266 the end of compilation, these warnings are not produced if the final
267 stage of compilation is not reached, for example because an error is
268 found during compilation, or because the :option:`-fsyntax-only` option is
271 .. option:: -Wno-selector
273 Default setting; overrides :option:`-Wselector`.
275 .. option:: -Wstrict-selector-match
279 Objective-C and Objective-C++ only
281 Warn if multiple methods with differing argument and/or return types are
282 found for a given selector when attempting to send a message using this
283 selector to a receiver of type ``id`` or ``Class``. When this flag
284 is off (which is the default behavior), the compiler omits such warnings
285 if any differences found are confined to types that share the same size
288 .. option:: -Wno-strict-selector-match
290 Default setting; overrides :option:`-Wstrict-selector-match`.
292 .. option:: -Wundeclared-selector
296 Objective-C and Objective-C++ only
298 Warn if a ``@selector(...)`` expression referring to an
299 undeclared selector is found. A selector is considered undeclared if no
300 method with that name has been declared before the
301 ``@selector(...)`` expression, either explicitly in an
302 ``@interface`` or ``@protocol`` declaration, or implicitly in
303 an ``@implementation`` section. This option always performs its
304 checks as soon as a ``@selector(...)`` expression is found,
305 while :option:`-Wselector` only performs its checks in the final stage of
306 compilation. This also enforces the coding style convention
307 that methods and selectors must be declared before being used.
309 .. option:: -Wno-undeclared-selector
311 Default setting; overrides :option:`-Wundeclared-selector`.
313 .. option:: -print-objc-runtime-info
315 Generate C header describing the largest structure that is passed by