如何从终端为用 Swift 编写的应用程序构建可执行文件?
How do I build an executable for an application written in Swift from the terminal?
我想问的是如何在 Xcode 之外执行此操作。
在文本编辑器中编写的 Swift 脚本可以用 xcrun swift hello_world.swift
或 swift hello_world.swift
执行。它们也可以通过 shebang 设置为可执行文件和 运行。这与 Python.
这样的脚本语言类似
但是由于 Swift 是一种编译语言,我确信必须有一些方法可以使用 swift
、[=14= 通过终端构建 Swift 可执行文件],等等。有没有人发现任何关于这个的东西?令人惊讶的是,我什么也没找到。
你需要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
。
我想问的是如何在 Xcode 之外执行此操作。
在文本编辑器中编写的 Swift 脚本可以用 xcrun swift hello_world.swift
或 swift hello_world.swift
执行。它们也可以通过 shebang 设置为可执行文件和 运行。这与 Python.
但是由于 Swift 是一种编译语言,我确信必须有一些方法可以使用 swift
、[=14= 通过终端构建 Swift 可执行文件],等等。有没有人发现任何关于这个的东西?令人惊讶的是,我什么也没找到。
你需要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
。