I'm asking how to do this outside of Xcode.
我问的是如何在Xcode之外做到这一点。
A Swift script written in a text editor can be executed with xcrun swift hello_world.swift
or swift hello_world.swift
. They can also be set to executable and run with a shebang. This is parallel to a scripting language like Python.
可以使用xcrun swift hello_world.swift或swift hello_world.swift执行在文本编辑器中编写的Swift脚本。它们也可以设置为可执行文件并与shebang一起运行。这与Python之类的脚本语言并行。
But since Swift is a compiled language, I'm sure there has to be some way of building a Swift executable through the Terminal using swift
, clang
, etc. Has anyone found anything regarding this? I've surprisingly found nothing.
但由于Swift是一种编译语言,我确信必须有一些方法可以通过终端使用swift,clang等构建Swift可执行文件。有没有人发现任何有关此问题的内容?我惊奇地发现什么都没有。
1 个解决方案
#1
You need swiftc
:
你需要swiftc:
% cat > hello.swift << EOF
heredoc> println("Hello, world!")
heredoc> EOF
% swiftc hello.swift
% ./hello
Hello, world!
%
If you want to compile multiple files, the one you want to actually run on launch needs to be called main.swift
(in which case you probably also want to use -o executablename
).
如果要编译多个文件,那么您希望在启动时实际运行的文件需要被称为main.swift(在这种情况下,您可能还想使用-o executablename)。
Various options available via swiftc --help
. The most likely one you want to use is -O
to turn on the optimizer.
通过swiftc提供各种选项--help。您最想使用的是-O以打开优化程序。
Also, depending on your environment settings, you may need to use xcrun -sdk macosx swiftc
, if you are using Foundation
or other SDKs.
此外,根据您的环境设置,如果您使用的是Foundation或其他SDK,则可能需要使用xcrun -sdk macosx swiftc。
#1
You need swiftc
:
你需要swiftc:
% cat > hello.swift << EOF
heredoc> println("Hello, world!")
heredoc> EOF
% swiftc hello.swift
% ./hello
Hello, world!
%
If you want to compile multiple files, the one you want to actually run on launch needs to be called main.swift
(in which case you probably also want to use -o executablename
).
如果要编译多个文件,那么您希望在启动时实际运行的文件需要被称为main.swift(在这种情况下,您可能还想使用-o executablename)。
Various options available via swiftc --help
. The most likely one you want to use is -O
to turn on the optimizer.
通过swiftc提供各种选项--help。您最想使用的是-O以打开优化程序。
Also, depending on your environment settings, you may need to use xcrun -sdk macosx swiftc
, if you are using Foundation
or other SDKs.
此外,根据您的环境设置,如果您使用的是Foundation或其他SDK,则可能需要使用xcrun -sdk macosx swiftc。