• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
objective-c categories and static libraries?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

objective-c categories and static libraries?


  • Subject: objective-c categories and static libraries?
  • From: Tobias Sargeant <email@hidden>
  • Date: Thu, 10 Mar 2005 09:37:56 +1100

[If this belongs on a gcc specific list, could someone please
point me in the direction of an appropriate one? Thanks]

I don't know if this constitutes a bug, or is an expected
behaviour, or is a result of something I'm (not) doing, but
objective-c categories in static libraries don't appear to be
linked into executables built with them.

Details below.

Cheers,
Toby.

I've defined a small category on NSString which is implemented in
cat.m and defined in cat.h like so:

#import <Foundation/Foundation.h>

@interface NSString(Split)

- (NSArray *)componentsSeparatedByString: (NSString *)str
                               maxSplits: (int)nsplits
                               keepEmpty: (BOOL)keep_empty;
- (NSArray *)componentsSeparatedByCharacters: (NSCharacterSet *)set
                                   maxSplits: (int)nsplits
                                   keepEmpty: (BOOL)keep_empty;
- (NSArray *)componentsSeparatedByCharacterRuns: (NSCharacterSet *)set
                                      maxSplits: (int)nsplits
                                      keepEmpty: (BOOL)keep_empty;

@end


A small stub, in test.m:

#import "cat.h"

int main(int argc, char **argv) {
id pool = [[NSAutoreleasePool alloc] init];
NSLog(@"%@", [[@"foo bar baz"
componentsSeparatedByCharacterRuns: [NSCharacterSet whitespaceCharacterSet]
maxSplits: 1
keepEmpty: NO] description]);
[pool release];
}


Compiled like this, it works as expected:

mac1156:~/test sargeant$ gcc -O0 -gfull -o test1 test.m cat.m -framework Foundation
mac1156:~/test sargeant$ ./test1
2005-03-10 09:30:55.872 test1[3774] (foo, "bar baz")


However, compiled like this:

mac1156:~/test sargeant$ gcc -O0 -gfull -c cat.m
mac1156:~/test sargeant$ ar cq libcat.a cat.o
mac1156:~/test sargeant$ ranlib libcat.a
mac1156:~/test sargeant$ gcc -O0 -gfull -o test2 test.m -L. -lcat -framework Foundation
mac1156:~/test sargeant$ ./test2
2005-03-10 09:32:10.335 test2[3792] *** -[NSConstantString componentsSeparatedByCharacterRuns:maxSplits:keepEmpty:]: selector not recognized
2005-03-10 09:32:10.337 test2[3792] *** Uncaught exception: <NSInvalidArgumentException> *** -[NSConstantString componentsSeparatedByCharacterRuns:maxSplits:keepEmpty:]: selector not recognized
Trace/BPT trap


Checking with nm, the category simply doesn't get linked in the second case:

mac1156:~/test sargeant$ nm test1 | grep Split
000028fc t -[NSString(Split) componentsSeparatedByCharacterRuns:maxSplits:keepEmpty:]
000026e4 t -[NSString(Split) componentsSeparatedByCharacters:maxSplits:keepEmpty:]
000024c0 t -[NSString(Split) componentsSeparatedByString:maxSplits:keepEmpty:]
00000000 A .objc_category_name_NSString_Split
mac1156:~/test sargeant$ nm test2 | grep Split
mac1156:~/test sargeant$


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: windowWillResize:toSize: and windowDidResize: are not paired
  • Next by Date: Re: windowWillResize:toSize: and windowDidResize: are not paired
  • Previous by thread: Re: windowWillResize:toSize: and windowDidResize: are not paired
  • Next by thread: RE: objective-c categories and static libraries?
  • Index(es):
    • Date
    • Thread