Building an Executable

A package is considered as an executable if it contains a file named main.swift. The package manager will compile that file into a binary executable.

In this example, the package will produce an executable named Hello that outputs “Hello, world!”.

First create and enter a directory called Hello:

$ mkdir Hello
$ cd Hello

Now run the swift package’s init command with executable type:

$ swift package init --type executable

Compile the package by running the swift build command:

$ swift build

After the command finishes, the built products will be available in the .build directory. Run the Hello program with the following command:

$ .build/debug/Hello
Hello, world!

As a next step, let’s define a new sayHello(name:) function in a new source file, and have the executable call that instead of calling print(_:) directly.