【问题标题】:How do I build an executable for an application written in Swift from the terminal?如何从终端为用 Swift 编写的应用程序构建可执行文件?
【发布时间】:2015-05-05 06:16:11
【问题描述】:

我在问如何在 Xcode 之外执行此操作。

在文本编辑器中编写的 Swift 脚本可以使用 xcrun swift hello_world.swiftswift hello_world.swift 执行。它们也可以设置为可执行并使用 shebang 运行。这类似于 Python 等脚本语言。

但由于 Swift 是一种编译语言,我确信必须有某种方法可以使用 swiftclang 等通过终端构建 Swift 可执行文件。有人找到任何相关信息吗?令人惊讶的是,我什么也没找到。

【问题讨论】:

    标签: macos swift terminal


    【解决方案1】:

    你需要swiftc:

    % cat > hello.swift << EOF
    heredoc> println("Hello, world!")
    heredoc> EOF
    % swiftc hello.swift 
    % ./hello 
    Hello, world!
    %
    

    如果你想编译多个文件,你想在启动时实际运行的文件需要被称为main.swift(在这种情况下你可能还想使用-o executablename)。

    通过swiftc --help 提供各种选项。您最有可能使用-O 来打开优化器。

    此外,根据您的环境设置,如果您使用的是Foundation 或其他 SDK,您可能需要使用 xcrun -sdk macosx swiftc

    【讨论】:

    • 适用于一般情况,但在添加像 import Foundation 这样的行时失败,我收到输出:&lt;unknown&gt;:0: error: cannot load underlying module for 'CoreGraphics' &lt;unknown&gt;:0: note: did you forget to set an SDK using -sdk or SDKROOT? &lt;unknown&gt;:0: note: use "xcrun -sdk macosx swift" to select the default OS X SDK installed with Xcode 遵循给出的一般建议不起作用。还有其他见解吗?
    • 当您发布此内容时,我正在编辑 :)
    • 我如何创建函数并调用她?
    • 如何使用 Podfile?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多