Reputation: 64477
I have a class from an external library that I want to extend with a category. However, this class uses several static variables, such as:
static SomeClass* someClass;
The category method I'm extending must make a call to this class, as in
-(void) categoryMethod
{
[someClass someMethod];
}
If I do that the compiler will complain:
'someClass' was not declared in this scope
So I figured I probably need to declare the static SomeClass as extern:
extern SomeClass* someClass;
However, that leads to the following linker error:
"_someClass", referenced from:
_someClass$non_lazy_ptr in UsingSomeClass.o
(maybe you meant: _someClass$non_lazy_ptr)
Symbol(s) not found
I think I'm missing something simple and stupid. I can use the static library just fine. I'm guessing that the static variables are local to the class I want to extend, is there any way to tell the linker that those are the static variables I'm meaning to use?
Or is it just not possible to extend an Objective-C class that uses static variables (respectively where my category needs to use these static variables)?
PS: since I'm linking the static library which contains the class I want to extend, I did originally face the problem that needs to be solved with the Other Linker Flags -ObjC and -all_load. But I've been past that, it's not the problem, I can write categories for other classes in that static library just fine.
Upvotes: 0
Views: 638
Reputation: 27900
A static
variable in C (and, therefore, in Objective-C) cannot be referenced outside the compilation unit that defines it. It's an internal symbol that you can't refer to with an "extern".
So, no, there isn't any way your category method can access that static variable.
Note also that this usage of the word "static" has nothing to do with static vs. dynamic libraries.
Upvotes: 3