Building a static library with cocoapods

前端 未结 3 510
孤独总比滥情好
孤独总比滥情好 2021-01-31 05:59

I am trying to build a static library that has different dependencies (AFNetworking for example) specified in a Podfile. I don\'t want the dependencies to be included in the fin

3条回答
  •  温柔的废话
    2021-01-31 06:14

    Referenced libraries are not (by default) included in the static library product. The linker conflict you're seeing is more likely the result of both your static library and the client app both using the default (implicit) Pod target.

    Every Cocoapods-generated target includes a "Pods-target-dummy.m" file that is compiled into the product; if you use the default Pods target, it's just called "Pods-dummy.m". When both the library and client use the default target, the identical symbols produced from compiling the dummy files will cause a link error.

    I tried a variation of Craig's answer myself, and found that the link_with statement is also responsible for hooking up the xcconfig generated by Cocoapods, which provides the compiler flags that control the header search path. You can add the xcconfig (or the header search path project settings) manually, of course, but I went looking for a repeatable solution for my team.

    My solution is to create an explicit target for the library, with a name that is unlikely to cause conflicts with a client project (for example, the name of the library):

    target 'XYZLibrary' do
        pod 'AFNetworking', '2.5.2'
        ...
    end
    

    You can include a link_with statement within the target block if the name of the static library target (in your Xcode project) is different, but if there's only one target, I usually prefer to use the same name in both places, making link_with unnecessary.

    If you have a unit test target, create two separate targets. (I currently def a set of common pods that are used in both targets, since abstract targets are not currently an option, but they may be one day.) It looks like this:

    def common_pods
      pod 'AFNetworking', '2.5.2'
    end
    
    target 'XYZLibrary' do
      common_pods
    end
    
    target 'XYZLibraryTests' do
      common_pods
    end
    

    The key is to not have any pod elements in the root of the Podfile, so that Cocoapods won't generate a default target. That way, each product gets a unique "Pods-target-dummy.m", and there's no conflict when those object files are linked together.

提交回复
热议问题