5

When I upgrade my Xcode to 14, my app crashed and Get an error message: dyld: Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib

It's only happen on devices with iOS version below 13,like iOS 12/11,

Tom Peter
  • 53
  • 1
  • 3

4 Answers4

4

As per https://developer.apple.com/forums/thread/714795, Apple suggested adding -Wl,-weak-lswiftCoreGraphics to the linker flags.

The problem was that without this flag your app will expect libswiftCoreGraphics.dylib to be at /usr/lib/swift/libswiftCoreGraphics.dylib on the phone. Because the dylib isn't there on older iOS versions, you'll get error like

EXC_CRASH (SIGABRT)
Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Description: DYLD, Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib 

Adding the flag tells the linker to treat this library as a weak linker flag. At the launch(or load) time, the dylib will be searched relative to the @rpath instead of hard coded /usr/lib/swift path.

You can learn more about rpath and how that helps dyld find the dylibs here

After running otool -L on the app I see few more libraries pointing to their /usr/lib/swift version but all of them are weak references e.g,

    /usr/lib/swift/libswiftCoreMIDI.dylib (compatibility version 1.0.0, current version 6.0.0, weak)
    /usr/lib/swift/libswiftCoreML.dylib (compatibility version 1.0.0, current version 1436.0.14, weak)
    /usr/lib/swift/libswiftDataDetection.dylib (compatibility version 1.0.0, current version 723.0.0, weak)
    /usr/lib/swift/libswiftFileProvider.dylib (compatibility version 1.0.0, current version 730.0.125, weak)
    /usr/lib/swift/libswiftOSLog.dylib (compatibility version 1.0.0, current version 4.0.0, weak)
...

The only library with non-weak reference was libswiftCoreGraphics before adding the linker flag.

/usr/lib/swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 120.100.0)

After adding the linker flag it appears as:

@rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 15.0.0, weak)
A. K.
  • 34,395
  • 15
  • 52
  • 89
  • 1
    It works for me and your explanation makes me clear. Thanks very much. – Daemonson Dong Sep 26 '22 at 03:14
  • 1
    @A.K I am not sure, wouldnt it be more wise to add `/usr/lib/swift/` with @executable_path to the LD_RUNPATH_SEARCH_PATH to make it search in all possible places? instead of weak linking. What will happen if he try to embed and SDK that depend on this libs the weak link wont help here, no? – Mike.R Sep 26 '22 at 14:57
  • I think `/usr/lib/swift/` is already added and that is why the setup was working for iOS>12. But on iOS-12.1 the library isn't there. Making `libswiftCoreGraphics` relative to @rpath the loader is searching for all places as you mentioned. – A. K. Sep 26 '22 at 16:12
1

most likely an interoperability issue between Xcode 14 and older iOS version devices. It has been reported multiple times in GitHub issues. Check this post in Apple Developer Forum.

So for solutions: add libSwiftCoreGraphics.tbd to your project's Frameworks,Libraries,and Embeded Contents.

If you are using pods or SPM, also check the updates from the author. For example, SnapKit just upadted their podfile 5 days ago

enter image description here

kakaiikaka
  • 3,530
  • 13
  • 19
  • This solution does not work for me. There's another [ongoing thread](https://developer.apple.com/forums/thread/714795) on Apple Developer Forums, saying "this is an issue on the Apple side of things." – denkeni Sep 21 '22 at 11:16
0

With Xcode14.1 RC 2 this issue has been fixed.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
0

The framework you are trying to compile might be dynamic framework. Try changing your "Embed" content of your framework from "Do not Embed" to "Embed & Sign" this might fix your crash enter image description here

Marolean James
  • 589
  • 4
  • 7