【问题标题】:macOS Command Line Tool with Swift Cocoa Framework: Library not loaded带有 Swift Cocoa 框架的 macOS 命令行工具:未加载库
【发布时间】:2015-04-19 02:30:37
【问题描述】:

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

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 时,目标运行时出现以下警告:

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 问题,还是我错过了某种设置?

【问题讨论】:

  • 虽然类似,但stackoverflow.com/a/24345546/58107 中的答案并不适用,因为命令行工具不附带“嵌入式二进制文件”配置窗格。
  • @IAmDav 我确实已经看到了这个问题。请注意,错误消息是不同的:Reason: no suitable image found. Did find:。此外,最受欢迎的答案还建议使用EMBEDDED_CONTENT_CONTAINS_SWIFT,我在我的问题中也提到过。
  • 这是与您的情况一样带有确切错误消息的 SO 问题:stackoverflow.com/questions/26104975/… 建议将 @executable_path/Frameworks 添加到目标的“运行路径搜索路径”。
  • 我还没有完成这项工作,并提交了一个 rader (rdar://20094031)。我已经通过在我的项目中包含源文件来解决这个问题。

标签: swift xcode command-line-interface


【解决方案1】:

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

SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES
SWIFT_FORCE_STATIC_LINK_STDLIB = NO

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

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

【讨论】:

  • 这绝对有效,因为它停止将 Swift 标准库直接放入可执行文件中,从而确保可执行文件使用共享动态库,同时避免两次发送库(在我的情况下节省了 8 MB)。谢谢!
  • 这是黄金。令人惊讶的是,Xcode 实际上没有构建设置来执行此操作。
  • $(TOOLCHAIN_DIR)/usr/lib/swift/macosx 仅在安装 Xcode 时存在,对吗?如果分发 Swift 可执行文件,则未安装 Xcode 的用户将缺少 Swift 标准库。 Swift标准库的版本也不能匹配吗?
  • 我能够删除SWIFT_FORCE_STATIC_LINK_STDLIB = NO,而没有任何明显的影响,因此其他设置似乎消除了一个。
  • @bouke 在 Swift 5 之前将其用于生产代码时要非常小心!在我们在 Swift 5 中获得 ABI 稳定性之前,链接到 stdlib 的操作系统版本并不能保证在与您正在使用的 Swift 版本不直接对齐的操作系统版本上工作。这可能应该添加到答案中。
【解决方案2】:

在您的“命令行工具”目标中..设置以下...

LD_RUNPATH_SEARCH_PATHS = @executable_path

(或者正如Xcode 所说的那样,“Runtime Search Paths”)设置这个,@rpath本质上,说..“嘿,在任何文件夹中查看‘工具’正在运行”用于动态加载的库。 (在这种情况下,它将是您的 build 文件夹。

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

【讨论】:

  • 构建文件夹不包含@rpath/libswiftCore.dylib,它不是第三方库。它是 Swift 运行时的一部分。该库可能嵌入在我的可执行文件中(测量 15 MB)?
  • 我也有同样的问题。构建使用 swift 的框架,将 EMBEDDED_CONTENT_CONTAINS_SWIFT 设置为 NO,使用该框架的构建目标无法找到 Swift 库。在 EMBEDDED_CONTENT_CONTAINS_SWIFT 设置为 YES 的情况下构建框架,您会收到“在两者中实现”错误。
  • 你得到我的投票。注意评论想法,如果命令行工具捆绑在可可应用程序的Resources 文件夹中,并且框架通常在Frameworks 中,我应该在 LD_RUNPATH_SEARCH_PATHS 中添加什么?
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2017-07-28
    • 2019-03-18
    • 2019-07-02
    相关资源
    最近更新 更多