使用Cocoapods构建静态库时防止重复的符号 [英] Prevent duplicate symbols when building static library with Cocoapods

查看:431
本文介绍了使用Cocoapods构建静态库时防止重复的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我已经看到许多有关Cocoapods和静态库的问题,但大多数问题似乎都假设您最终将拥有一个包含静态库和最终目标应用程序的工作区.

While I've seen many questions dealing with Cocoapods and static libraries, most of them seem to assume you'll eventually have a single workspace with your static library and end target app.

在我的场景中,我正在构建一个静态库.更具体地说,我正在破解一个MyLib.framework供用户使用.我想真的要用Cocoapods管理MyLib.framework的依赖关系,但是当我的图书馆的消费者 也使用Cocoapods时,这会带来很多麻烦.

In my scenario, I am building a static library. More specifically, I'm hacking a MyLib.framework for users to consume. I'd really like to manage MyLib.framework's dependencies with Cocoapods, but it creates many pain points when consumers of my library also use Cocoapods.

例如,我的图书馆有一个AFNetworking依赖关系,我可以通过Cocoapods对其进行管理.当我构建库时,它会链接到包含AFNetworking的libPods.a以及一些虚拟"文件/对象.如果我的框架的用户还使用Cocoapods来构建应用,他们将看到以下内容:

For example, my library has an AFNetworking dependency which I manage with Cocoapods. When I build my library, it links in libPods.a which includes AFNetworking, as well as some "dummy" files/objects. If users of my framework also use Cocoapods to build their app, they'll see something like this:

duplicate symbol _OBJC_METACLASS_$_PodsDummy_Pods in:
    /Users/erikkerber/Dropbox/Projects/MillMain/MyLib.framework/BuddySDK(Pods-dummy.o)
    /Users/erikkerber/Library/Developer/Xcode/DerivedData/MillMain-fngfqhlslygksgcfuciznkpqfrbr/Build/Products/Debug-iphonesimulator/libPods.a(Pods-dummy.o)
duplicate symbol _OBJC_CLASS_$_PodsDummy_Pods in:
    /Users/erikkerber/Dropbox/Projects/MillMain/MyLib.framework/BuddySDK(Pods-dummy.o)
    /Users/erikkerber/Library/Developer/Xcode/DerivedData/MillMain-fngfqhlslygksgcfuciznkpqfrbr/Build/Products/Debug-iphonesimulator/libPods.a(Pods-dummy.o)
ld: 2 duplicate symbols for architecture i386

我想如果他们要添加一个AFNetworking依赖项,他们也会得到与AFNetworking相关的重复符号.

I imagine if they were to add an AFNetworking dependency, that they would also get duplicate symbols relating to AFNetworking.

我计划最终还与Cocoapods一起分发MyLib,但我也希望能够分发MyLib.framework本身.

I plan to eventually distribute MyLib with Cocoapods as well, but I also want to be able to distribute a MyLib.framework itself.

在使Cocoapods对任何潜在用户安全的同时,有什么办法可以在我的图书馆中使用Cocoapods?

Is there any way to use Cocoapods with my library while making Cocoapods safe to any potential user?

推荐答案

简而言之,分发预构建库的唯一好方法是包括任何依赖项,而是将其保留给用户. IE.在您的示例中,您将指导用户如何还将AFNetworking添加到他们的项目中.基本上,dummy文件也是如此.

In short, the only good way of distributing prebuilt libraries is by not including any of the dependencies, but leaving that up to the user. I.e. in your example, you would instruct your users how to also add AFNetworking to their project. The same basically applies regarding the dummy files.

话虽如此,您当然可以使用多个预先构建的变体:

Having said that, you could of course go for multiple prebuilt variants:

  • 包括所有依赖项.
  • 仅包括您lib的来源,而依赖项则取决于用户.

我们一直在讨论创建一个插件来生成独立的静态库,以达到您想要的目的,但这尚未开始,可能会花费一些时间. (直到有人/任何人有时间.)

We have been talking about creating a plugin to produce standalone static libraries, for the purpose that you want, but that’s as of yet not started and will probably take a little while longer. (Until someone/anyone has the time.)

作为一种解决方法,您可以使用Podfile的 post_install挂钩完全删除虚拟文件. (无论如何,只有诸如Testflight之类的非源代码库才需要这些.)类似于以下内容:

As a workaround, you could use your Podfile’s post_install hook to remove the dummy files altogether. (these are only needed for non-source libs like Testflight anyways.) E.g. something like the following:

post_install do |installer|
  installer.project.targets.each do |target|
    source_files = target.source_build_phase.files
    dummy = source_files.find do |file|
      # TODO Fix this to the actual filename
      # puts "File: #{file.file_ref.name}"
      file.file_ref.name == 'TheDummyFile.m'
    end
    puts "Deleting source file #{dummy.inspect} from target #{target.inspect}."
    source_files.delete(dummy)
  end
end

这是未经测试的代码.

post_install挂钩产生CocoaPods安装程序对象,您可以从中获取Pods.xcodeproj目标,您可以从中找到文档

The post_install hook yields the CocoaPods installer object, from which you can get the Pods.xcodeproj targets, for which you can find documentation here. From there you can drill down and do anything you like to the project, which is saved to disk after running this hook.

这篇关于使用Cocoapods构建静态库时防止重复的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆