19

My app was running perfectly without any errors or warnings yesterday. But when I tried to run it today, the build failed - I haven't changed anything in the code:

Undefined symbol: __swift_FORCE_LOAD_$_XCTestSwiftSupport

How to fix this, and why would something like this occur suddenly when it was working before?

And this is an issue I've been experiencing with XCode a lot lately. My code will be running smoothly without any errors, but then XCode will randomly start throwing errors when I relaunch it at a different time - without making any changes in the actual code.

I have included some photos and the full text of those photos:

InventoryApp 3 issues
    Warning
        Could not find or use auto-linked library 'XCTestSwiftSupport'
        Could not find or use auto linked framework 'XCTest'
    Error
        Undefined symbol: __swift FORCE LOAD $ XCTestSwiftSupport

Photo

ld: warning: Could not find or use auto-linked library 'XCTestSwiftSupport'
ld: warning: Could not find or use auto-linked framework
'XCTest'
Undefined symbols for architecture arm64:
    "__swift_FORCE_LOAD_$_XCTestSwiftSupport", referenced from:
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in Button.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ViewModel.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in Inventory.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in AddView.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ScannerView.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in RegisterView.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ContentView.o
        ...
       (maybe you meant: __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Photo

James Risner
  • 5,451
  • 11
  • 25
  • 47
ela16
  • 740
  • 1
  • 2
  • 13
  • 1
    Clear derived data ? Cleaned build ? – amar Nov 09 '21 at 06:46
  • Yes I tried both. – ela16 Nov 09 '21 at 21:52
  • If your code ran fine a few days ago, but suddenly one day it started reporting errors, something must have changed. If it was not a code change, maybe it was Xcode itself, or maybe the project configuration. Be aware of these things. – ziggear Nov 10 '21 at 13:28
  • 1
    So I was able to get rid of the error by going to app settings > Build Phases > Link Binary With Libraries > add the linked library "XCTest.framework". HOWEVER, even though the build succeeds, when the app launches, it aborts: 0_abort_with_payload. And if I remove the library, the build starts failing again. – ela16 Nov 10 '21 at 22:05
  • I solved this other issue by navigating to target app > Build Settings > Linking > Other Linker Flags > editing or adding -weak_framework "XCTest". https://stackoverflow.com/questions/20340763/xcode-5-0-2-dyld-library-not-loaded-rpath-xctest-framework-versions-a-xctest – ela16 Nov 11 '21 at 00:00

5 Answers5

37

This condition also occurs if you add

import XCTest

to a file in a non-test bundle.

Spiff
  • 783
  • 8
  • 9
  • 7
    Omg thank you. Since latest Xcode will automatically add the import statements; I had accidentally created an XCTestCase in my non-test file by accident and it added the import without me realizing it. – Hedylove Feb 22 '22 at 04:05
  • 2
    Yes, this can easily happen with Xcode 13 automatic imports. If you mistype something it might import a framework, like XCTest, and you may not know it. That's what happened to me. – Devin Pitcher Mar 28 '22 at 02:41
  • For me, I unintendedly import a module (Cuckoo) that is only available in test target, not main target. I removed that import and it works now. – Hlung Aug 05 '22 at 02:10
21

I solved this error by navigation to target app settings > Build Phases > Link Binary With Libraries > add the linked library "XCTest.framework".

If afterwards your app starts crashing, aborting with the following errors:

dyld: Library not loaded: @rpath/XCTest.framework/Versions/A/XCTest

0_abort_with_payload

Then you can take a look at the suggestions on this thread: Xcode 5.0.2 dyld: Library not loaded: @rpath/XCTest.framework/Versions/A/XCTest

The solution that worked for me was: Navigate to target app > Build Settings > Linking > Other Linker Flags > editing or adding -weak_framework "XCTest"

ela16
  • 740
  • 1
  • 2
  • 13
  • 1
    In my case, after seeing "dyld: Library not loaded: @rpath/XCTest.framework/Versions/A/XCTest", I turned the Status to Optional for the XCTest.framework library under Link Binary With Libraries. That solved the problem. – Cable W Dec 17 '21 at 07:11
  • 1
    This feels like a last resort if nothing else works. It may be harmless, but ideally you don't want to send out your production app with XCTest linked in. It's better to track down the dependency that is causing Xcode to think it needs an XCTest symbol. I think @Spiff's answer pinpoints what is likely the root cause in many cases. – Devin Pitcher Mar 28 '22 at 16:33
14

This error randomly started to occur on my Xcode project today. I changed the setting.

PROJECT -> Build Settings -> Build Options -> Enable Testing Search Paths

from No to Yes, and the build succeeded.

Edit:...

This setting is on by default if the project is created with a testing target included. In my case I believe I added the testing target later so this setting will have been false. Although, I can't say why it didn't break as soon as I added the testing target.

See: https://xcodebuildsettings.com

Seamus
  • 1,107
  • 10
  • 22
2

None of the above worked for me. In my case, a couple of mock files were targeted to both the framework and unit testing bundle. I just unticked the framework one to get rid of this error on the inspector.

Yekta Sarıoğlu
  • 1,435
  • 2
  • 12
  • 20
1

I solved this error by navigation to target app settings > Build Phases > Link Binary with Libraries > add the linked library "XCTest.framework".

The action above is totally the same as the solutions above, though the issue "Library not loaded" appeared raised.

I solved the "Library not loaded" issue by setting up the framework to be optional while linked library. [enter image description here][1]

It works for me in Xcode 13.2.1.