How do I build an executable for an application written in Swift from the terminal?

橙三吉。 提交于 2020-01-03 02:53:15

问题


I'm asking how to do this outside of 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.

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.


回答1:


You need 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).

Various options available via swiftc --help. The most likely one you want to use is -O to turn on the optimizer.

Also, depending on your environment settings, you may need to use xcrun -sdk macosx swiftc, if you are using Foundation or other SDKs.



来源:https://stackoverflow.com/questions/30045495/how-do-i-build-an-executable-for-an-application-written-in-swift-from-the-termin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!