Prevent Duplicate Symbols When Building Static Library with Cocoapods

Avoid duplicate symbols with static library and cocoapods

Third party static libraries should never link other third party static libraries themselves. All libraries should be linked together at the end by the final application link step. If the cocoapod spec is calling for one static library to include another, then it is incorrect and needs to be fixed.

For more on this, see this question, as well as the questions it links:

ObjC: How to compile static library that includes optional classes that depend on a third party library

Prevent duplicate symbols when building static library with Cocoapods

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:

  • Include all dependencies.
  • Only include your lib’s source, leave dependencies up to the user.

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.)

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

This is untested code.

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.



Related Topics



Leave a reply



Submit