开发者

IPhone: Adding Category to CoreLocation threw link error. Why?

I added a category to CLLocation in CoreLocation but kept getting a link error.

I search around and through process of TRY IT AND BE DAMNED I fixed the problem by adding -framework CoreLocation to Other Linker Flags

But dont know why?

I'm guessing its something to do with CoreLocation being and external framework.

The Code: CLLocation+DistanceComparison.h

#import < Foundation/Foundation.h >
#import < CoreLocation/CoreLocation.h >

static CLLocation * referenceLocation;
@interface CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other;
@end

CLLocation+DistanceComparison.m

#import "CLLocation+DistanceComparison.h"
#import <CoreLocation/CoreLocation.h>
@implementation CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other {
    CLLocationDistance thisDistance = [self getDistanceFrom:referenceLocation];
    CLLocationDistance thatDistance = [other getDistanceFrom:referenceLocation];
    if (thisDistance < thatDistance) { return NSOrderedAscending; }
    if (thisDistance > thatDistance) { return NSOrderedDescending; }
     return NSOrderedSame;
    }
@end

Build Error:

 Ld build/Debug-iphonesimulator/SortedLocations.app/Sorted开发者_如何学编程Locations normal i386  
 cd /Users/clearbrian/Documents/Development/IPhoneDevelopment/034.SortedLocations/SortedLocations  
 setenv MACOSX_DEPLOYMENT_TARGET 10.5  
 setenv PATH   "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"  
 /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot   /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk -L/Users/clearbrian/Documents/Development/IPhoneDevelopment/034.SortedLocations/SortedLocations/build/Debug-iphonesimulator -F/Users/clearbrian/Documents/Development/IPhoneDevelopment/034.SortedLocations/SortedLocations/build/Debug-iphonesimulator -filelist /Users/clearbrian/Documents/Development/IPhoneDevelopment/034.SortedLocations/SortedLocations/build/SortedLocations.build/Debug-iphonesimulator/SortedLocations.build/Objects-normal/i386/SortedLocations.LinkFileList -mmacosx-version-min=10.5 -framework Foundation -framework UIKit -framework CoreGraphics -o /Users/clearbrian/Documents/Development/IPhoneDevelopment/034.SortedLocations/SortedLocations/build/Debug-iphonesimulator/SortedLocations.app/SortedLocations  

 Undefined symbols:  
 ".objc_class_name_CLLocation", referenced from:  
 literal-pointer@__OBJC@__cls_refs@CLLocation in RootViewController.o  
 ld: symbol(s) not found  
 collect2: ld returned 1 exit status  

Getting link error when we added category

maybe because its external framework

got help from

hg clone https://traillocation.googlecode.com/hg/ traillocation

They added Foundation in Other Linker Flags for one of their targets

so I tried adding CoreLocation see attempt 2

ATTEMPT 1 : didnt work - see attempt 2

NOTE: to use this category on the 3.0 device, the -all_load linker flag

must be set in users of this library due to a toolkit bug.

See Categories in static library for iPhone device 3.0

Right Click on project name in Groups and Files pane

Get Info

Build tab

type Other Linker Flags in search

when found

Double click in right column

add -all_load

ATTEMPT 2 -

Right Click on project name in Groups and Files pane

Get Info

Build tab

type Other Linker Flags in search

when found

Double click in right column

add -framework CoreLocation

close

if you reopen the params are on two lines, this is ok

-framework

CoreLocation

Clean all targets

Build

seemed to work


Most likely you didn't include the CoreLocation framework in your "Link binary with libraries" phase of your target. To do so, double click your target, go to the first tab, hit the + button in the bottom left, find the CoreLocation framework in the list, and hit "Add".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜