在私有框架,应用程序和扩展中使用静态库中的类的正确方法是什么?我的示例项目可以在这里找到
https://github.com/keithyipkw/framework
Ld /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator/FrameworkApp.app/FrameworkApp normal x86_64 cd /Users/keithyip/Documents/Workspace/FrameworkApp export IPHONEOS_DEPLOYMENT_TARGET=8.4 export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.4.sdk -L/Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator -F/Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator -filelist /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Intermediates/FrameworkApp.build/Debug-iphonesimulator/FrameworkApp.build/Objects-normal/x86_64/FrameworkApp.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -objc_abi_version -Xlinker 2 -fobjc-arc -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=8.4 /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator/SDK.framework/SDK -Xlinker -dependency_info -Xlinker /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Intermediates/FrameworkApp.build/Debug-iphonesimulator/FrameworkApp.build/Objects-normal/x86_64/FrameworkApp_dependency_info.dat -o /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator/FrameworkApp.app/FrameworkApp Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_GAI",referenced from: objc-class-ref in AppDelegate.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command Failed with exit code 1 (use -v to see invocation)
该符号在.a中是全局的,但在SDK中是本地的
$nm -a /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator/SDK.framework/SDK | grep '_OBJC_CLASS_$_GAI' 00000000000d94e0 s _OBJC_CLASS_$_GAI
在第三次提交中,我将.a添加到app目标.该应用程序运行但有警告
objc[3743]: Class GAI is implemented in both /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator/SDK.framework/SDK and /Users/keithyip/Library/Developer/CoreSimulator/Devices/752A7B8E-405E-4403-BDD8-A168613774B1/data/Containers/Bundle/Application/D16B5121-2DA9-452B-9574-95B35AE3E197/FrameworkApp.app/FrameworkApp. One of the two will be used. Which one is undefined.
$nm -a /Users/keithyip/Library/Developer/Xcode/DerivedData/FrameworkApp-bpzqozighjdtncegosucvgelzagc/Build/Products/Debug-iphonesimulator/SDK.framework/SDK | grep '_OBJC_CLASS_$_GAI' 00000000000d94e0 s _OBJC_CLASS_$_GAI $nm -a /Users/keithyip/Library/Developer/CoreSimulator/Devices/752A7B8E-405E-4403-BDD8-A168613774B1/data/Containers/Bundle/Application/D16B5121-2DA9-452B-9574-95B35AE3E197/FrameworkApp.app/FrameworkApp | grep '_OBJC_CLASS_$_GAI' 0000000100032c88 s _OBJC_CLASS_$_GAI
深入挖掘nm -m,libGoogleAnalyticsServices.a与其他静态库不同.链接到动态库中的其他库是没有问题的.
0000000000002b20 (__DATA,__objc_data) private external _OBJC_CLASS_$_GAI
似乎不可能公开这些符号
clang: error: invalid argument '-keep_private_externs' not allowed with '-dynamiclib'
解决方法
通常,您只需将静态库链接到动态库即可.但是,在这种情况下,.a中的符号是私有外部(visibility = hidden),不可能将libGoogleAnalyticsServices.a包含到动态库中,并使用iOS上动态库外部的符号.这个时刻解决它的选择是
>将动态库更改为静态库
>重构代码以从动态库中删除GA
>为GA创建包装类,以便被调用者不需要链接到GA
如果将来有人遇到类似的情况,其他选择是
>要求供应商公开符号
>检查iOS是否存在“objcopy –globalize-symbols”等效项.当我写这篇文章时,最接近的是objconv.它支持OS X,但不支持iOS. An objcopy equivalent for Mac / iPhone?