我构建了一个链接到其他框架的静态库,尤其是 CoreLocation。我想使用 iOS 5 提供的功能但要兼容 4.3。
当我在 4.3 的 iOS 设备上启动它并出现以下错误时,我的应用程序在启动时崩溃:
Date/Time: 2012-08-22 16:44:53.900 +0200
OS Version: iPhone OS 4.3.3 (8J3)
Report Version: 104
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x00000001, 0xe7ffdefe
Crashed Thread: 0
Dyld Error Message:
Symbol not found: _UIKeyboardDidChangeFrameNotification
这篇文章中描述了我遇到的问题:iOS 4 app 在 iOS 3.1.3 上启动时崩溃:找不到符号:__NSConcreteStackBlock。
但是在构建静态库时如何处理呢?设置 -weak-lSystem 标志时无法编译。这是一个跟踪:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: -dynamic not specified, -all_load invalid
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: -dynamic not specified the following flags are invalid: -ObjC
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -weak-lSystem
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -weak-lSystem is not an object file (not allowed in a library)
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/usr/lib/libxml2.2.dylib is a dynamic library, not added to the static library
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/usr/lib/libxml2.2.dylib is a dynamic library, not added to the static library
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/usr/lib/libz.dylib is a dynamic library, not added to the static library
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/usr/lib/libz.dylib is a dynamic library, not added to the static library
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool failed with exit code 1
解决
请参阅下面接受的答案,不要忘记在 Xcode 中将不同版本中使用的库标记为可选。例如:我为 iOS 5 使用 UIKit 新通知,但我的部署目标是 4.3,所以我需要将此库标记为可选以使事情正常工作。CoreLocation CLGeocoder 新的 iOS 5 类也是如此。