34

The following error is thrown when trying to run my target:

dyld: Library not loaded: @rpath/libswiftCore.dylib
  Referenced from: x/Xcode/DerivedData/x/Build/Products/Debug/PriorityQueue.framework/Versions/A/PriorityQueue
  Reason: image not found

My target is a Command Line Tool (macOS) written in Swift. I'm using a Cocoa Framework (PriorityQueue) written in Swift. When I set EMBEDDED_CONTENT_CONTAINS_SWIFT to YES on the Framework, the target runs with the following warnings:

objc[99144]: Class _TtC10Foundation15NSSimpleCString is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftFoundation.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtC10Foundation16NSConstantString is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftFoundation.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtC10Foundation15NSFastGenerator is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftFoundation.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCE10FoundationCSo12NSDictionary9Generator is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftFoundation.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs18_EmptyArrayStorage is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs24_ContiguousArrayStorage1 is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs36_NativeSetStorageKeyNSEnumeratorBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs26_NativeSetStorageOwnerBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs18_CocoaSetGenerator is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs43_NativeDictionaryStorageKeyNSEnumeratorBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs33_NativeDictionaryStorageOwnerBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs25_CocoaDictionaryGenerator is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs17NonObjectiveCBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs17_stdlib_AtomicInt is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs19_SwiftNativeNSArray is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs24_SwiftNativeNSDictionary is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs17_SwiftNativeNSSet is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs24_SwiftNativeNSEnumerator is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs31_stdlib_ReturnAutoreleasedDummy is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs20_SwiftNativeNSString is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs19_NSContiguousString is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs40_SwiftNativeNSArrayWithContiguousStorage is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs21_SwiftDeferredNSArray is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs27_ContiguousArrayStorageBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _TtCSs13VaListBuilder is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class SwiftObject is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _SwiftNativeNSArrayBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _SwiftNativeNSDictionaryBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _SwiftNativeNSSetBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _SwiftNativeNSStringBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.
objc[99144]: Class _SwiftNativeNSEnumeratorBase is implemented in both x/PriorityQueue.framework/Versions/A/Frameworks/libswiftCore.dylib and x/MyTargetName. One of the two will be used. Which one is undefined.

And then crashes with (could be unrelated):

MyTargetName[99144:2157492] *** NSForwarding: warning: object 0x1014346a0 of class 'Swift._NSContiguousString' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[Swift._NSContiguousString fileSystemRepresentation]

Is this an issue with Xcode that should be reported, or am I missing a setting somehow?

Bouke
  • 11,768
  • 7
  • 68
  • 102
  • While similar, the answer in http://stackoverflow.com/a/24345546/58107 doesn't apply as Command Line Tool don't come with the "Embedded Binaries" configuration pane. – Bouke Feb 18 '15 at 07:03
  • Did you check this? http://stackoverflow.com/questions/26024100/dyld-library-not-loaded-rpath-libswiftcore-dylib – David V Feb 18 '15 at 09:37
  • @IAmDav I did already see that question. Note that the error message is different: `Reason: no suitable image found. Did find:`. Also, the most upvoted answer also suggests using `EMBEDDED_CONTENT_CONTAINS_SWIFT`, which I've also mentioned in my question. – Bouke Feb 18 '15 at 16:49
  • 1
    Here are the SO question with exact error message as in your case: http://stackoverflow.com/questions/26104975/dyld-library-not-loaded-rpath-libswiftcore-dylib-image-not-found It is suggesting to add @executable_path/Frameworks to "Runpath Search Paths" of your target. – David V Feb 19 '15 at 06:05
  • Were you ever able to get this to work? I am trying to create a swift dylib and I cannot get the thing to load for the life of me. – Tyler Cloutier Mar 09 '15 at 01:18
  • 3
    I haven't got this to work yet, and filed a rader (rdar://20094031). I've worked around this by including the source files in my project. – Bouke Mar 09 '15 at 19:25

3 Answers3

25

Swift Package Manager sets the following, which resolved this issue for me in another project:

SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES
SWIFT_FORCE_STATIC_LINK_STDLIB = NO

Note that the search path was set to:

LD_RUNPATH_SEARCH_PATHS = $(TOOLCHAIN_DIR)/usr/lib/swift/macosx @executable_path
Bouke
  • 11,768
  • 7
  • 68
  • 102
  • 1
    This absolutely works, because it stops putting the Swift standard library right into the executable, thus making sure the executable uses the shared dynamic library while avoiding shipping the library twice (8 MB savings in my case). Thanks! – Alex Repty Feb 08 '17 at 13:02
  • This is gold. it's amazing that Xcode literally has no Build Setting to do this. – algal Oct 31 '17 at 20:56
  • 2
    `$(TOOLCHAIN_DIR)/usr/lib/swift/macosx` only exists when Xcode is installed, correct? If distributing a Swift executable, the Swift standard library would be missing for users who don't have Xcode installed. Also couldn't there be a version mismatch of the Swift standard library as well? – duncanc4 May 24 '18 at 08:56
  • I was able to remove `SWIFT_FORCE_STATIC_LINK_STDLIB = NO` without any noticeable effect, so that one seems to be obviated by the other setting. – juliand665 Jan 05 '19 at 23:40
  • @bouke Be very careful about using this for production code before Swift 5! Before we get ABI stability in Swift 5, linking against the OS version of the stdlib is not guarenteed to work on a version of the OS not directly aligned with the Swift version you're using. This should probably be added to the answer. – Matthew Seaman Feb 07 '19 at 16:34
8

In your "Command Line Tool" Target.. set the following...

LD_RUNPATH_SEARCH_PATHS = @executable_path

(Or as Xcode calls them, "Runtime Search Paths") Setting this, the @rpath, in essence, says.. "hey, look in whatever folder the 'tool' is running in" for the dynamically loadable libs. (In this case it will be your build folder.

Other useful @rpath combinations involve similarly relative paths to the executable (which may also be a library or framework binary, in some cases), via @loader_path. Say if the lib was bundled within a framework.. It may require the LD_RUNPATH_SEARCH_PATHS of @loader_path/Frameworks, etc.

Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • 1
    The build folder doesn't contain `@rpath/libswiftCore.dylib`, it's not a third party library. It's part of the Swift runtime. The lib is probably embedded in my executable file (measuring 15 MB)? – Bouke Mar 02 '15 at 16:19
  • 6
    I have exactly the same issue. Building a Framework that uses swift, with EMBEDDED_CONTENT_CONTAINS_SWIFT set to NO, the built target that uses the framework fails to find the Swift libraries. Build the Framework with EMBEDDED_CONTENT_CONTAINS_SWIFT set to YES, and you get the "implemented in both" error. – timbo Nov 24 '15 at 01:16
  • you get my vote. Care to comment thought, if the command line tool is bundled in a cocoa app's `Resources` folder, and the framework in usual `Frameworks`, what do i put in LD_RUNPATH_SEARCH_PATHS? – user1244109 Nov 22 '16 at 21:19
2

The above worked for me. the two user build flags and then setting the following in runtime search paths.

LD_RUNPATH_SEARCH_PATHS = $(TOOLCHAIN_DIR)/usr/lib/swift/macosx @executable_path
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
kpeck
  • 21
  • 1