Reputation: 4810
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.
Upvotes: 1
Views: 2792
Reputation: 40965
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.
Upvotes: 5