On nextstep, with NeXT's Objective-C, the linker has a flag,
-ObjC
telling it to load any object encountered in a
library, even if it does not help resolving, if it merely defines an
Objective-C class or category. Problem solved.
On other systems, with GNU's Objective-C, where the linker is blissfully unaware of Objective-C, such an option does not exist. It is, however, fairly easy to obtain Objective-C semantics on these machines.
Every class definition in an Objective-C source file causes the
definition of a global symbol in the output file. For a class
Foo
, the symbol __objc_class_name_Foo
is
defined. Similarly, for the category Too
of the class
Foo
, the symbol
__objc_category_name_Foo_Too
is defined.
Now, if a class method is invoked of the Bar
class, an
undefined reference is added to the object file to, in this case,
__objc_class_name_Bar
. Such a reference is also output
for the superclass of a class being defined. Thus, basically,
classes are resolved just as effectively as functions normally are.
There are, however, two problems:
For example, to force inclusion of the Foo (Too)
category
mentioned above, one could add the following function to an object
file known to always be included:
void mylib_get_objc_linking (void *x) { extern int __objc_category_name_Foo_Too; if (!x) return; mylib_get_objc_linking (&__objc_category_name_Foo_Too); }The
return
statement is needed in case the function is
actually called, which in turn might be needed if there is not a
`the single always-referenced object file'. If the
function actually is called, the x
argument should be
0, obviously. The if
can not be omitted, to prevent
the compiler from thinking to be smart and deleting the invocations
following the unconditional return
.
I know nothing about Stepstone's Objective-C, so I can't even tell if this problems exists on that platform.