9

I am trying to link a Cocoapods generated static library to multiple targets in my workspace. When I do this I get a duplicate symbols error in the linking phase when building. This makes sense as I am linking the library twice.

How do I get around this? Should I be linking to the main target only and include the headers paths to the Pods in the shared targets? How would I do this in Cocoapods? I could set the header paths to the Pods manually but seems to defeat the point of using pod install.

Below is my workpsace setup. It consists of a main project and multiple static libraries as their own xcode projects which have shared code. I link the products of the two static library projects to the main target and they automatically become dependencies of the main target.

shared1.xcodeproj -> target shared1
shared2.xcodeproj -> target shared2 
main.xcodeproj -> target main

This is my Podfile:

workspace 'Main.xcworkspace'
xcodeproj 'Main.xcodeproj'
xcodeproj 'Shared1.xcodeproj'
platform :ios

target :ThirdParty, :exclusive => true do
    link_with ['main', 'shared1']

    pod 'MKNetworkKit'
    pod 'SBJSON'
    ...
end
Fergal Rooney
  • 1,330
  • 2
  • 18
  • 31
  • The solution I am working with for now is to just link to the main target only and manually edit the shared targets build settings to add a recursive header search path to the Pods BuildHeaders directory. – Fergal Rooney Jun 25 '13 at 13:29
  • Is this still the best solution you've come up with? – Daniel Wood Jul 25 '13 at 10:16
  • This is the solution I came up with. It's basically the opposite of yours and requires less work: http://stackoverflow.com/a/17869668/106703 – Daniel Wood Jul 25 '13 at 22:36
  • I don't like the idea of having to update the build phase settings every I run pod install, especially in a source controlled environment. Thanks for the suggestion though!! – Fergal Rooney Jul 26 '13 at 15:16
  • I'm with you on that one, but until Cocoapods resolve the issue this is the best solution I could find. – Daniel Wood Jul 26 '13 at 21:53
  • Is there an open issue with CocoaPods for this? I'm hitting basically the same problem as both of you. – cbowns Nov 06 '13 at 00:23
  • I'm amazed that there's no satisfactory answer here. Cocoapods is a great idea for managing dependencies from a Pod project but it sure makes configuring linking static libraries non-trivial. – fatuhoku Jul 10 '14 at 17:18

3 Answers3

4

Have you tried adding this in the podfile:

link_with 'Target1', 'Target2'

I've got 5 different targets in mine this way and it seems to work nicely.

Mike
  • 9,765
  • 5
  • 34
  • 59
  • It works, but how to spefify only the pods that I need in Target2 as I don't need all pods that are available to Target1? – Mohamed Saleh Dec 20 '15 at 07:03
1

I have just been through this horrible situation and fixed it up - I now have a workspace that will build via command line or xcode that has 4 different targets with different bundle ID's. The problem with your approach above is that you end up with linking madness as each target will build the pods. What I did was:

  1. Create project with one target and pods configured.
  2. Copy the target 3 times and change bundle id's etc. using this link: http://swwritings.com/post/2013-05-20-concurrent-debug-beta-app-store-builds
  3. Create a workspace level scheme for each of your targets.
  4. Create a workspace level scheme for your pods build.
  5. Edit your schemes and turn off 'Find Implicit Dependencies'.
  6. Make your schemes depend on your pods scheme (build tab in edit schemes).

My podfile looks like this - just the same as it was at the start:

target 'test-target' do
    pod 'HockeySDK'
    pod 'RestKit', '~> 0.23.1'
    pod 'RKXMLReaderSerialization', :git => 'https://github.com/RestKit/RKXMLReaderSerialization.git', :branch => 'master'
    pod 'NSLogger'
    pod 'Parse-iOS-SDK'
end

I did have quite a few problems when building where it kept telling me it couldn't link to the pods build which I resolved by changing the 'Library Search Paths' in build settings to:

$(PROJECT_DIR)/Build/Products/$(CONFIGURATION)-$(PLATFORM_NAME)

It's a bit of a strange path with /Build/Products but that seems to be what Pods does out of the box and if you fight against it things get ugly.

It's hard to write a detailed how-to as it's taken me 5 days of experimentation to get the magic build working in Xcode, xcodebuild and our Teamcity CI server.

One last gotcha - turn of build paralleization!!! (edit/schemes/build) ... or your pods will in time for the application build to see them

Oly Dungey
  • 1,603
  • 19
  • 20
0

I've come across this. The way I've fixed it is by removing the dependencies for the main project. As the main project links with the shared project, it gets access to the dependencies automatically.

workspace 'Main.xcworkspace'
xcodeproj 'Main.xcodeproj'
xcodeproj 'Shared1.xcodeproj'
platform :ios

target :ThirdParty, :exclusive => true do
    link_with ['shared1']

    pod 'MKNetworkKit'
    pod 'SBJSON'
    ...
end
Trah Divad
  • 51
  • 5