Cocoapods: Tests fail to build because of linker error when using xcodebuild but work fine in Xcode

Created on 31 Mar 2019  Â·  3Comments  Â·  Source: CocoaPods/CocoaPods

Report

What did you do?

With a very basic project that contains:

  • an iOS framework target
  • an iOS host app target
  • an iOS test target that uses the host app for its tests

I have a Podfile like this:

platform :ios, '12.2'

target 'TestsHostTests' do
  pod 'Kiwi', '~> 3.0.0'
end

I've run pod install and then try and run the tests from the command line using:

xcodebuild -scheme "TestsHost" -destination "platform=iOS Simulator,name=iPhone Xs,OS=12.2" -project "MyFramework.xcodeproj" test

What did you expect to happen?

I expect to be able to run the tests successfully from both Xcode and the command line (using xcodebuild)

What happened instead?

The tests run successfully from Xcode but fail to build when using xcodebuild because:

$ xcodebuild -scheme "TestsHost" -destination "platform=iOS Simulator,name=iPhone Xs,OS=12.2" -project "MyFramework.xcodeproj" test

note: Using new build system
note: Planning build
note: Using build description from disk
Ld /Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Products/Debug-iphonesimulator/TestsHost.app/PlugIns/TestsHostTests.xctest/TestsHostTests normal x86_64 (in target: TestsHostTests)
    cd /Users/ham/Desktop/xcodebuild_problem/MyFramework
    export IPHONEOS_DEPLOYMENT_TARGET=12.2
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -bundle -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk -L/Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Products/Debug-iphonesimulator -L/Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Products/Debug-iphonesimulator/Kiwi -F/Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Products/Debug-iphonesimulator -F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -filelist /Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Intermediates.noindex/MyFramework.build/Debug-iphonesimulator/TestsHostTests.build/Objects-normal/x86_64/TestsHostTests.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -mios-simulator-version-min=12.2 -dead_strip -bundle_loader /Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Products/Debug-iphonesimulator/TestsHost.app/TestsHost -Xlinker -object_path_lto -Xlinker /Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Intermediates.noindex/MyFramework.build/Debug-iphonesimulator/TestsHostTests.build/Objects-normal/x86_64/TestsHostTests_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -Xlinker -objc_abi_version -Xlinker 2 -fobjc-arc -fobjc-link-runtime -ObjC -lKiwi -framework XCTest -framework XCTest -Xlinker -sectcreate -Xlinker __TEXT -Xlinker __entitlements -Xlinker /Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Intermediates.noindex/MyFramework.build/Debug-iphonesimulator/TestsHostTests.build/TestsHostTests.xctest-Simulated.xcent -lPods-TestsHostTests -Xlinker -dependency_info -Xlinker /Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Intermediates.noindex/MyFramework.build/Debug-iphonesimulator/TestsHostTests.build/Objects-normal/x86_64/TestsHostTests_dependency_info.dat -o /Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Products/Debug-iphonesimulator/TestsHost.app/PlugIns/TestsHostTests.xctest/TestsHostTests
ld: warning: directory not found for option '-L/Users/ham/Library/Developer/Xcode/DerivedData/MyFramework-cesqbkfpkalfmpawzaiudyklowsm/Build/Products/Debug-iphonesimulator/Kiwi'
ld: library not found for -lKiwi
clang: error: linker command failed with exit code 1 (use -v to see invocation)

CocoaPods Environment

Stack

   CocoaPods : 1.7.0.beta.3
        Ruby : ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]
    RubyGems : 3.0.2
        Host : Mac OS X 10.14.4 (18E226)
       Xcode : 10.2 (10E125)
         Git : git version 2.20.1
Ruby lib dir : /Users/ham/.rvm/rubies/ruby-2.6.1/lib
Repositories : master - https://github.com/CocoaPods/Specs.git @ 92082566f79cc26c7241fc0411c97e23a8101c92

Installation Source

Executable Path: /Users/ham/.rvm/gems/ruby-2.6.1/bin/pod

Plugins

cocoapods-deintegrate : 1.0.4
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.0
cocoapods-stats       : 1.1.0
cocoapods-trunk       : 1.3.1
cocoapods-try         : 1.1.0

Podfile

platform :ios, '12.2'

target 'TestsHostTests' do
  pod 'Kiwi', '~> 3.0.0'
end

Project that demonstrates the issue

https://github.com/hamchapman/MyFrameworkTest

Most helpful comment

I figured out a solution for my use case. I need to use - workspace MY_WORKSPACE.xcworkspace instead of -project MY_PROJECT.xcodeproj in the xcodebuild invocation, i.e.

xcodebuild -scheme "TestsHost" -destination "platform=iOS Simulator,name=iPhone Xs,OS=12.2" -workspace "MyFramework.xcworkspace" test

It should have been more obvious to me given you have to open the workspace, and not the project, when working with a target that relies on Pods.

I'm closing the issue as I'm not sure the other issues mentioned in this issue are the same and if they are then hopefully the same change fixes those issues too.

All 3 comments

I have a similar problem.

Does it work for you with versions of cocoa pods later than 1.5.3 ? (1.5.3 is my last working version)

On the current project I have a lot of private frameworks as pods. After installing Xcode 10.2 we have to update CocoaPods from 1.5.3 where everything worked flawlessly to 1.6.1 where we experienced the same issue with framework Test targets: dyld: Library not loaded. pod lib lint works fine and that was the main point – fix CI issues – but the existing Xcode project with Unit tests target fails with this error.

Podfile:

source 'https://github.com/CocoaPods/Specs.git'
source 'PRIVATE_SPECS_REPO'

platform :ios, '9.0'
inhibit_all_warnings!
use_frameworks!

target 'Framework' do
  pod 'RxAlamofire'
  pod 'RxSwift'

  target 'FrameworkTests' do
    inherit! :search_paths
    pod 'OHHTTPStubs/Swift'
  end
end

What is interesting, changing inherit! to :complete resolved this issue and now tests runs in Xcode. But shouldn't the Test target inherit parent dependencies even with :search_paths as it was with CocoaPods 1.5.3?

I figured out a solution for my use case. I need to use - workspace MY_WORKSPACE.xcworkspace instead of -project MY_PROJECT.xcodeproj in the xcodebuild invocation, i.e.

xcodebuild -scheme "TestsHost" -destination "platform=iOS Simulator,name=iPhone Xs,OS=12.2" -workspace "MyFramework.xcworkspace" test

It should have been more obvious to me given you have to open the workspace, and not the project, when working with a target that relies on Pods.

I'm closing the issue as I'm not sure the other issues mentioned in this issue are the same and if they are then hopefully the same change fixes those issues too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

k06a picture k06a  Â·  3Comments

tlandsmancars picture tlandsmancars  Â·  3Comments

spencerkohan picture spencerkohan  Â·  3Comments

Mingmingmew picture Mingmingmew  Â·  3Comments

sonu5 picture sonu5  Â·  3Comments