具有 Swift Cocoa 框架的 macOS 命令行工具:未加载库

macOS Command Line Tool with Swift Cocoa Framework: Library not loaded

尝试 运行 我的目标时抛出以下错误:

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

我的目标是用 Swift 编写的命令行工具 (macOS)。我正在使用用 Swift 编写的 Cocoa 框架(PriorityQueue)。当我在 Framework 上将 EMBEDDED_CONTENT_CONTAINS_SWIFT 设置为 YES 时,目标 运行s 出现以下警告:

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.

然后崩溃(可能无关):

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

这是应该报告的 Xcode 问题,还是我不知何故缺少设置?

在您的 "Command Line Tool" 目标中..设置以下...

LD_RUNPATH_SEARCH_PATHS = @executable_path

(或者如 Xcode 所称,“Runtime Search Paths”)设置这个,@rpath本质上,说.. "hey, look in whatever folder the 'tool' is running in" 用于可动态加载的库。 (在这种情况下,它将是您的 build 文件夹。

其他有用的 @rpath 组合涉及可执行文件(在某些情况下也可能是库或框架二进制文件)的类似相对路径,通过 @loader_path。假设 lib 是否捆绑在一个框架内。它可能需要 @loader_path/FrameworksLD_RUNPATH_SEARCH_PATHS,等等。

Swift 包管理器设置了以下内容,这在另一个项目中为我解决了这个问题:

SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES
SWIFT_FORCE_STATIC_LINK_STDLIB = NO

请注意,搜索路径设置为:

LD_RUNPATH_SEARCH_PATHS = $(TOOLCHAIN_DIR)/usr/lib/swift/macosx @executable_path

以上对我有用。两个用户构建标志,然后在运行时搜索路径中设置以下内容。

LD_RUNPATH_SEARCH_PATHS = $(TOOLCHAIN_DIR)/usr/lib/swift/macosx @executable_path