26

I've built a Swift framework and now I'm trying to start building a Swift iOS application that will use that framework. I'm getting this error:

dyld: Library not loaded: @rpath/libswiftSwiftOnoneSupport.dylib
  Referenced from: /Users/tdean/Library/Developer/Xcode/DerivedData/NFLApplication-ejmafvjrlqgjaabggwvadjarjjlg/Build/Products/Debug-iphonesimulator/NFLStatsModel.framework/NFLStatsModel
  Reason: image not found

I've scoured SO and found similar reports and tried the fixes listed there, including:

  • Clearing out my DerivedData folder
  • Restarting Xcode and the iPhone simulator
  • Ensuring that Always Embed Swift Standard Libraries = YES is set, both in my framework and my application's build settings
  • Ensuring that Enable Bitcode=NO is set, both in my framework and my application's build settings
  • Ensuring that Runpath Search Paths is set to @executable_path/Frameworks, both in my framework and my application's build settings
  • Copied all the libswift files from my Xcode installation into a local copy within my project, and added a custom build phase to copy those files into the frameworks folder.

In every case, I get the same error when I try to run my application.

  • Xcode Version 8.1 (8B62)
  • Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1)
Bista
  • 7,869
  • 3
  • 27
  • 55
Tim Dean
  • 8,253
  • 2
  • 32
  • 59

8 Answers8

46

I eventually got this working using a mix of fixes. I'm not sure if all of them are needed, but I'm documenting what seemed to work for me here, just in case anyone else can benefit by what I've found.

  1. I have set Always Embed Swift Standard Libraries to a value of YES in the build settings tab for both my Swift framework and in the Swift application that uses the framework.
  2. I have added Foundation.framework to the Linked Frameworks and Libraries section of the general tab for both my Swift framework and in the Swift application that uses the framework.
  3. I have added Foundation.framework to the Embedded Binaries section of the general tab for the Swift application that uses the framework.

With all 3 of these settings in place, I am able to build and run my application without encountering this error.

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • 1
    THANK YOU! I spent two hours setting up a demo app/framework feature of a production app I'm working on. Twice while building things I encountered this exact error. For some reason I could loop (iterate) through an array of custom UIButtons in the framework, but the moment I tried to actually access something in the loop I got this error. FYI: I tested each thing and the *only* one I needed to add was #1. This new feature has been developed solely in Xcode 8 and is working (I don't know why) in a branch off of things, but not in any other newly created project - until now. –  Mar 30 '17 at 01:36
  • 6
    It was enough for me to fulfill the 1st point of your instruction. Thank you. – A.Kant Jun 04 '17 at 09:27
  • 3
    Point 1 alone solved it for me. I find it strange that you have to put it on the app AND on the linked framework. XCode's dependencies management is a source of perplexity. Any other way to make it automatic? – Francis Pierot Jan 24 '18 at 11:11
  • I had this problem on a unit test target. Adding Foundation.framework on the _Link Binary with Libraries_ of the test target was enough for me. – Jano Aug 12 '18 at 19:18
  • Point 1 solved it for me.Of note, this was happening with an embedded custom framework – Renato Sep 27 '18 at 10:14
  • If you're using CocoaPods, set this flag for Pods project as well ‍♂️ – farzadshbfn Oct 04 '18 at 15:35
  • I also had this issue using Xcode 11 and trying to run the tests against a simulator on iOS 11. It seems a lot of things can cause this issue – José Manuel Sánchez Oct 02 '19 at 14:32
27

This might not be the case for everyone, but I solved it by actually writing some code in the main target.

I had an empty project consisting of a framework and a test target, and when running tests I was getting this error. Apparently Swift is pretty smart to detect that you don't actually need this library and does not link to libswiftSwiftOnoneSupport.dylib.

The fix is just to add some code, I just added:

class Test {
    func a() { print ("something") }
}

and libswiftSwiftOnoneSupport.dylib got linked.

S2dent
  • 939
  • 6
  • 9
  • 2
    "Apparently Swift is pretty smart to detect that you don't actually need this library and does not link to libswiftSwiftOnoneSupport.dylib" Too smart for its own good, since you evidently do. – Kevin Dec 07 '18 at 05:32
  • Just lost 3 hours on that. Warning: I had just struct declaration in main target and I was still getting this error. Adding Test class with function made error disappear (!!!). Crazy. – Piotr Jan 12 '19 at 17:34
  • Did you find a different solution for that? Same happened for me, and its strange – Martin Mlostek Jan 24 '19 at 14:42
  • No, since I actually needed my framework to have some code inside. You could in theory force link to it, but are you sure you need that? As long as your framework will have some code inside, you should be good to go. – S2dent Jan 25 '19 at 10:01
  • This worked for me as well, in a unit test target on iOS 11 & Xcode 10.2. Worth noting that there was no issue on iOS 12. Bizarre. – mokagio Apr 30 '19 at 01:17
  • Thanks! Turned out a dev had put in a test with no actual code in the methods yet (new module). Disabled that test target in the scheme and boom no more error. – CommaToast Feb 21 '20 at 22:17
14

After several days of being stuck with this issue I finally found something that worked for me; hopefully this will help others too.

Turns out that specifically using print() anywhere in the code will somehow force libswiftSwiftOnoneSupport.dylib to be loaded and the issue will go away.

I'm using Xcode 10.1, Swift 4.2 and the pod that was giving me this issue was Nimble.

BTW, I am aware of @S2dent's suggestion to "just add some code" but in my case my framework already had several different classes so it didn't help me.

LuisCien
  • 6,362
  • 4
  • 34
  • 42
  • 2
    Works for me to... this was the only thing what worked, but how? Why? Can anyone explain it? – F3R1 Feb 21 '19 at 18:44
  • I can't believe it. This is also the correct answer for me. I just added a random `print()` to a random Swift file in my tests! This smells like a Xcode bug. – Buju Jun 07 '19 at 10:08
9

How are you installing your dependencies?

I had a similar issue:

dyld: Library not loaded: @rpath/libswiftSwiftOnoneSupport.dylib Referenced from: <internal framework> Reason: image not found

It turned out to be related to Swift whole-module optimization.

Using Carthage as a dependency manager, they were being compiled for Release, and thus compiled with whole-module optimization, which Xcode suggested I turn on. Running the app on the simulator compiles it for Debug. I'm guessing that dynamic frameworks cannot be at a different level of optimization from the app running it.

The solution was to explicitly specify the configuration I wanted Carthage to build for. (carthage bootstrap --configuration Debug) Oh, and cleaning my build folder, of course.

weissazool
  • 185
  • 7
  • Thanks for the suggestion, but I was not installing any dependencies nor using Carthage. I have a Swift framework of my own creation in one Xcode project. I have a Swift application of my own creation in another Xcode project. I have both projects inside a single Xcode workspace, and I have the framework being referenced by the application. – Tim Dean Dec 16 '16 at 00:24
  • Try checking the Optimization Level in the build settings of both your framework and your app. – weissazool Dec 17 '16 at 11:48
  • Just cleaning my build folder worked for me. I always use option- Xcode > Product > Clean Build Folder – Kaydell Sep 24 '18 at 14:00
3

I had the same issue, adding the library (my own build one) to Linked Frameworks and Libraries in General tab of the app solved the issue.

Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • I was running into the same dylib linker error, but with the LinkKit library. Followed your instructions and rebuilt. Works fine now on XCode 12.2 building for iOS 14.2 – semore_1267 Dec 25 '20 at 01:13
0

You can also provide an Host Application to your test target if you don't want to add Foundation.framework to Linked Frameworks or Embedded Binaries

Arnaud Dorgans
  • 363
  • 1
  • 2
  • 14
0

You can solve this by setting "Always Embed Swift Standard Libraries" to "Yes" in the Build Settings of your target.

Joris Weimar
  • 4,783
  • 4
  • 33
  • 53
0

It is an dynamic linker error which links binary in load or runtime

[@rpath]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205