简体   繁体   English

Swift共享库是否为命令行应用程序静态链接?

[英]Swift shared libraries getting statically linked for command-line apps?

I'm trying to use an external framework with my Swift application for Mac OS X. The external framework also uses Swift, and so depends on the Swift shared libraries (for example, libswiftCore.dylib ). 我正在尝试将外部框架与我的Swift应用程序一起用于Mac OS X.外部框架也使用Swift,因此依赖于Swift共享库(例如, libswiftCore.dylib )。 This is verified by the command 这由命令验证

$ otool -L PromiseKit.framework/PromiseKit
PromiseKit.framework/PromiseKit:
    ...
    @rpath/libswiftCore.dylib (compatibility version 0.0.0, current version 0.0.0)

Looking up @rpath I see 查看@rpath我明白了

$ otool -l PromiseKit.framework/PromiseKit
      ...
      cmd LC_RPATH
  cmdsize 40
     path @executable_path/Frameworks (offset 12)

So at runtime I expect @rpath to resolve to @executable_path/Frameworks 所以在运行时我希望@rpath解析为@executable_path/Frameworks

The problem 问题

I get a runtime error 我收到运行时错误

dyld: Library not loaded: @rpath/libswiftCore.dylib
  Referenced from: .../PromiseKit.framework/Versions/A/PromiseKit
  Reason: image not found

Looking in the folder containing my built executable, I don't see a Frameworks folder. 查看包含我构建的可执行文件的文件夹,我看不到Frameworks文件夹。

Failed attempts to fix it 尝试修复它失败了

I tried setting EMBEDDED_CONTENT_CONTAINS_SWIFT to YES for my app, but this still doesn't create the Frameworks folder. 我尝试为我的应用程序设置EMBEDDED_CONTENT_CONTAINS_SWIFTYES ,但这仍然不会创建Frameworks文件夹。

I tried manually creating the Frameworks folder and copying in the Swift shared libraries (from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx . This fixes the original error, but now I see duplicate symbol warmings like this one: 我尝试手动创建Frameworks文件夹并在Swift共享库中复制(来自/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx 。这修复了原始错误, 现在我看到像这样的重复符号变暖:

objc[64445]: Class _TtC10Foundation15NSSimpleCString is implemented in both
   .../Frameworks/libswiftFoundation.dylib and
   .../myApp.
   One of the two will be used. Which one is undefined.

Question

Is it possible that the Swift libraries are being statically linked into my application? 是否有可能将Swift库静态链接到我的应用程序中? If so how do I turn that off and have XCode create the Frameworks folder version instead? 如果是这样,我如何关闭它并让XCode创建Frameworks文件夹版本?

I think the important point is that it's a command-line app. 我认为重要的是它是一个命令行应用程序。 I tried creating a regular app and adding the framework and I get the expected Frameworks folder within my .app package and it contains the Swift shared libraries 我尝试创建一个常规应用程序并添加框架,我在.app包中获得了预期的Frameworks文件夹,它包含Swift共享库

I can get a swift command-line tool with or without an external framework working, as long as: 我可以使用或不使用外部框架的快速命令行工具,只要:

  1. The framework copies the swift libraries into its Frameworks directory (optional) 框架将swift库复制到其Frameworks目录中(可选)
  2. The command-line tool doesn't generate a build failure trying to copy the swift libraries 尝试复制swift库时,命令行工具不会生成构建失败
  3. The tool runpath is configured with a path containing the swift libraries 工具运行路径配置了包含swift库的路径

To get a Framework to copy the swift libraries, set: 要获取框架以复制swift库,请设置:

"Embedded Content Contains Swift Code" = Yes

To avoid a build failure when a tool tries to copy the swift libraries into its non-existent Bundle Frameworks directory, set: 为避免在工具尝试将swift库复制到其不存在的Bundle Frameworks目录中时出现构建失败,请设置:

"Embedded Content Contains Swift Code" = Yes

To configure a tool's runpath, add one of the following to LD_RUNPATH_SEARCH_PATHS: 要配置工具的运行路径,请将以下其中一项添加到LD_RUNPATH_SEARCH_PATHS:

A. The swift libraries in the external framework: A.外部框架中的swift库:

@executable_path/PromiseKit.framework/Versions/A/Frameworks

Note that Xcode doesn't symlink Versions/A/Frameworks to Frameworks by default, like it does with Resources, Headers, and Modules. 请注意,默认情况下,Xcode不会将版本/ A /框架符号链接到框架,就像资源,标题和模块一样。

B. The swift libraries in the Xcode app: B. Xcode应用程序中的swift库:

$(DEVELOPER_DIR)/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx

This only works with a sufficiently similar Xcode installed on each machine the executable needs to run on. 这仅适用于在可执行文件需要运行的每台机器上安装的足够类似的Xcode。 But it doesn't require an external framework to get the tool to work. 但它不需要外部框架来使工具工作。

C. The swift libraries redistributed in the same directory as the executable: C. swift库在与可执行文件相同的目录中重新分发:

@executable_path

Or, if the executable is in a bin/ directory and the libraries are in a lib/ directory: 或者,如果可执行文件位于bin /目录中,并且库位于lib /目录中:

@executable_path/../lib

This can be redistributed, and doesn't require an external framework to get the tool to work. 这可以重新分发,并且不需要外部框架来使工具工作。 But it would require a manual copy files phase in Xcode to get the directory structure set up this way. 但它需要在Xcode中手动复制文件阶段才能以这种方式设置目录结构。


Note that if the tool is configured with: 请注意,如果工具配置为:

"Embedded Content Contains Swift Code" = Yes

Xcode throws the following build error: Xcode抛出以下构建错误:

2015-06-04 00:56:28.816 swift-stdlib-tool[5208:2453424] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager copyItemAtPath:toPath:error:]: destination path is nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff8ee4503c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff89f1c76e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8ee44eed +[NSException raise:format:] + 205
    3   Foundation                          0x00007fff926569b7 -[NSFileManager copyItemAtPath:toPath:error:] + 185
    4   swift-stdlib-tool                   0x000000010526b485 _Z13copyLibrariesP8NSStringS0_P19NSMutableDictionary + 853
    5   swift-stdlib-tool                   0x000000010526c60b main + 3915
    6   libdyld.dylib                       0x00007fff97d785c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Copying libswiftCore.dylib from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx to (null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM