如何从终端为Swift编写的应用程序构建可执行文件?

Jar*_*red 1 macos terminal swift

我问的是如何在Xcode之外做到这一点.

用文本编辑器编写的Swift脚本可以用xcrun swift hello_world.swift或执行swift hello_world.swift.它们也可以设置为可执行文件并与shebang一起运行.这与Python之类的脚本语言并行.

但是,由于雨燕是编译型语言,我敢肯定,必须有使用通过终端建设斯威夫特可执行的一些方式swift,clang等有没有人发现就这事?我惊奇地发现什么都没有.

Air*_*ity 5

你需要swiftc:

% cat > hello.swift << EOF
heredoc> println("Hello, world!")
heredoc> EOF
% swiftc hello.swift 
% ./hello 
Hello, world!
%
Run Code Online (Sandbox Code Playgroud)

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

提供各种选择swiftc --help.您最想要使用的是-O打开优化程序.

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