xcode 使用现有的静态库和 iOS 应用程序配置 Cocoapods
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16451635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring Cocoapods with an existing static library and iOS application
提问by user2393462435
I'm having trouble getting my workspace to compile correctly with Cocoapods. There are 3 projects in the workspace, each with their own target:
我无法使用 Cocoapods 正确编译我的工作区。工作区中有 3 个项目,每个项目都有自己的目标:
- libPods - Cocoapods static library with all the external dependencies
- libCommon - My static library where I keep all my shared code (base controllers, networking code, common UI, etc)
- myApp - My iOS application
- libPods - 具有所有外部依赖项的 Cocoapods 静态库
- libCommon - 我的静态库,我保存所有共享代码(基本控制器、网络代码、通用 UI 等)
- myApp - 我的 iOS 应用程序
Both libCommon and myApp require the external dependencies from the libPods. Originally I thought it would work like this:
libCommon 和 myApp 都需要来自 libPods 的外部依赖项。最初我认为它会像这样工作:
- libPods builds
- libCommon links against libPods and builds
- myApp links with libCommon and builds
- libPods 构建
- 针对 libPods 和构建的 libCommon 链接
- myApp 与 libCommon 链接并构建
In this scenario libCommon "owns" the pods, and then then myApp just links against libCommon like I've always done pre-Cocoapods... but apparently static libraries don't like to be linked with static libraries (I got a bunch of dynamic library errors). I read on a github issue somewhere that instead I should build libPods and libCommon and then myApp should link against both libraries. Right now my podfile looks something like this:
在这种情况下,libCommon “拥有” pod,然后 myApp 只是链接 libCommon 就像我一直在 Cocoapods 之前所做的那样......但显然静态库不喜欢与静态库链接(我得到了一堆动态库错误)。我在某个地方读到了一个 github 问题,我应该构建 libPods 和 libCommon,然后 myApp 应该链接到这两个库。现在我的 podfile 看起来像这样:
workspace 'MyApp.xcworkspace'
platform :ios, '5.0'
link_with ['Common', 'MyApp']
target 'MyApp' do
xcodeproj 'MyApp.xcodeproj'
pod 'AFNetworking', '1.1.0'
pod 'TTTAttributedLabel', '1.6.0'
pod 'JSONKit', '1.5pre'
pod 'Reachability', '3.1.0'
end
With this setup, myApp owns all the pods, and then in the libCommon build settings I specify the path to the pod headers. This builds OK until I attempt to use one of the classes in libCommon. Once I do that, I get one of those _OBJC_CLASS_$_Blah
errors (which tells me that although the headers are available, it's still not linked properly). When I try to manually link libCommon in "Build Phases" I get a bunch of duplicate symbol errors (which leaves me to believe it's already linked?). What the heck?
通过此设置,myApp 拥有所有 pod,然后在 libCommon 构建设置中指定 pod 标头的路径。在我尝试使用 libCommon 中的类之一之前,这可以正常构建。一旦我这样做了,我就会收到这些_OBJC_CLASS_$_Blah
错误之一(它告诉我,虽然标题可用,但它仍然没有正确链接)。当我尝试在“构建阶段”中手动链接 libCommon 时,我收到一堆重复的符号错误(这让我相信它已经链接了?)。有没有搞错?
What's the way to do this properly and what should my podfile look like?
正确执行此操作的方法是什么,我的 podfile 应该是什么样的?
回答by Fabio
CocoaPods creates an implicit root target which by default links with the first target of the project. As you are creating another target and the link_with option is not inherited by children target definitions your set up is not working. In order to make the link_with option you can move it inside the block of the MyApp target definition.
CocoaPods 创建一个隐式根目标,默认情况下与项目的第一个目标链接。由于您正在创建另一个目标,并且子目标定义未继承 link_with 选项,因此您的设置不起作用。为了使 link_with 选项,您可以将其移动到 MyApp 目标定义的块内。
As the Common target links with the Pods, if you link those with the MyApp it will result in the duplicate symbols error as the app links with Common. In this case you just need to make the headers available to the MyApp target. This is simple to do but there isn't a proper DSL yet so for the moment is a bit hacky as a solution (but supported).
由于 Common 目标与 Pod 链接,如果将它们与 MyApp 链接,则会导致应用程序与 Common 链接时出现重复符号错误。在这种情况下,您只需要使标头可用于 MyApp 目标。这很容易做到,但还没有合适的 DSL,所以目前作为解决方案有点麻烦(但支持)。
workspace 'MyApp.xcworkspace'
platform :ios, '5.0'
target 'Common' do
pod 'AFNetworking', '1.1.0'
pod 'TTTAttributedLabel', '1.6.0'
pod 'JSONKit', '1.5pre'
pod 'Reachability', '3.1.0'
target 'MyApp', :exclusive => true do
xcodeproj 'MyApp.xcodeproj'
end
end
回答by Daniel Wood
The solution I've adopted for this situation is as follows:
我针对这种情况采用的解决方案如下:
I set up the Podfile quite simply:
我非常简单地设置了 Podfile:
workspace 'MyApp.xcworkspace'
platform :ios, '5.0'
xcodeproj 'Common.xcodeproj'
pod 'AFNetworking', '1.1.0'
pod 'TTTAttributedLabel', '1.6.0'
pod 'JSONKit', '1.5pre'
pod 'Reachability', '3.1.0'
target 'MyApp' do
xcodeproj 'MyApp.xcodeproj'
# specific dependencies
end
This way the Common lib and MyApp are set up correctly to use all the dependencies. However, this will still cause duplicate symbols. The way to get around that is to simply delete libPods.a from the Common project's Build Phase. This is fine because we don't really want to link to the Cocoapods static lib to our static lib anyway. All the correct dependencies will be linked when you build the app, and all the correct header paths are set up in the .xccconfig files so Xcode/AppCode will still provide all your auto-completions and everything will compile.
这样,Common lib 和 MyApp 就被正确设置为使用所有依赖项。但是,这仍然会导致重复符号。解决这个问题的方法是简单地从 Common 项目的 Build Phase 中删除 libPods.a。这很好,因为我们真的不想将 Cocoapods 静态库链接到我们的静态库。当您构建应用程序时,所有正确的依赖项都将被链接,并且所有正确的头路径都在 .xccconfig 文件中设置,因此 Xcode/AppCode 仍将提供您所有的自动完成,并且一切都会编译。
You'll need to delete the libPods.a each time you run pod install
which is a bit of a pain, but it might be better suffering that than managing all the dependencies manually.
每次运行时都需要删除 libPods.a pod install
,这有点痛苦,但与手动管理所有依赖项相比,这可能会更好。
Update:I'm working on this as I write and I've just noticed it's important not to use the Linker Flags Cocoapods sets up within your static library. By default, my static lib had overridden their value with no value but Cocoapods warns against this and advises you use $(inherited). Just ignore it.
更新:我在写这篇文章时正在处理这个问题,我刚刚注意到不要使用静态库中设置的链接器标志 Cocoapods 很重要。默认情况下,我的静态库已无值覆盖了它们的值,但 Cocoapods 对此发出警告并建议您使用 $(inherited)。忽略它。