【问题标题】:IPhone: Adding Category to CoreLocation threw link error. Why?iPhone:将类别添加到 CoreLocation 引发链接错误。为什么?
【发布时间】:2024-01-23 18:58:01
【问题描述】:

我在 CoreLocation 的 CLLocation 中添加了一个类别,但一直收到链接错误。
我四处搜索并通过 TRY IT AND BE DAMNED 的过程我通过添加解决了这个问题 -framework CoreLocation 到其他链接器标志
但是不知道为什么?
我猜它与 CoreLocation 和外部框架有关。

代码: 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

构建错误:

ld build/Debug-iphonesimulator/SortedLocations.app/SortedLocations 正常 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 未定义的符号: “.objc_class_name_CLLocation”,引用自: RootViewController.o 中的文字指针@__OBJC@__cls_refs@CLLocation ld:未找到符号 collect2: ld 返回 1 个退出状态

添加类别时出现链接错误
可能是因为它的外部框架

得到了帮助
hg clone https://traillocation.googlecode.com/hg/ traillocation
他们在其他链接器标志中为他们的一个目标添加了 Foundation
所以我尝试添加 CoreLocation 参见尝试 2

尝试 1:没有工作 - 请参阅尝试 2

注意:要在 3.0 设备上使用此类别,-all_load 链接器标志
由于工具包错误,必须在此库的用户中设置。
Categories in static library for iPhone device 3.0

在组和文件窗格中右键单击项目名称
获取信息
构建选项卡
在搜索中键入其他链接器标志
找到时
双击右栏
添加 -all_load

尝试 2 -
在组和文件窗格中右键单击项目名称
获取信息
构建选项卡
在搜索中键入其他链接器标志
找到时
双击右栏
添加 -framework CoreLocation
关闭
如果您重新打开参数在两行,这没关系
-框架
核心位置
清除所有目标
构建
似乎有效

【问题讨论】:

    标签: iphone objective-c xcode linker


    【解决方案1】:

    很可能您没有在目标的“将二进制文件与库链接”阶段中包含 CoreLocation 框架。为此,请双击您的目标,转到第一个选项卡,点击左下角的 + 按钮,在列表中找到 CoreLocation 框架,然后点击“添加”。

    【讨论】: