Swift’s build system provides a convention-based system for building libraries and executables, and sharing code across different projects.
These examples assume you have made swift available in your path; see Installing for more information. Once available, you can invoke the build system using swift build:
$ swift build --help
OVERVIEW: Build sources into binary products
...
swift available in your path; see Installing for more information. Once available, you can invoke the build system using swift build:$ swift build --help
OVERVIEW: Build sources into binary products
...
Creating a Package
To create a new Swift package, first create and navigate to a new directory named Hello:
$ mkdir Hello
$ cd Hello
Every package must have a manifest file called Package.swift in its root directory. If the manifest file is blank, the package manager will build the package using conventional defaults. Create an empty manifest using:
$ touch Package.swift
When using the defaults, the package manager expects all source code to be contained in aSources/ subdirectory. Create that with:
$ mkdir Sources
Hello:$ mkdir Hello
$ cd Hello
Package.swift in its root directory. If the manifest file is blank, the package manager will build the package using conventional defaults. Create an empty manifest using:$ touch Package.swift
Sources/ subdirectory. Create that with:$ mkdir Sources
Building an Executable
By default, a directory that contains a file called main.swift will compile that file into a binary executable with the name of the package.
In this example, the package will produce an executable named Hello that outputs “Hello, world!”.
Create a file called main.swift in the Sources/ directory and using your editor of choice, enter the following code:
print("Hello, world!")
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.
main.swift will compile that file into a binary executable with the name of the package.Hello that outputs “Hello, world!”.main.swift in the Sources/ directory and using your editor of choice, enter the following code:print("Hello, world!")
swift build command:$ swift build
.build directory. Run the Hello program with the following command:$ .build/debug/Hello
Hello, world!
sayHello(name:) function in a new source file, and have the executable call that instead of calling print(_:) directly.Working with Multiple Source Files
Create a new file in the Sources/ directory called Greeter.swift, and enter the following code:
func sayHello(name: String) {
print("Hello, \(name)!")
}
The sayHello(name:) function takes a single String argument and prints our “Hello” greeting before, substituting the word “World” with the function argument.
Now, open main.swift again, and replace the existing contents with the following code:
if Process.arguments.count != 2 {
print("Usage: hello NAME")
} else {
let name = Process.arguments[1]
sayHello(name: name)
}
Rather than using a hardcoded name as before, main.swift now reads from the command line arguments. And instead of invoking print(_:) directly, main.swift now calls thesayHello(name:) method. Because the method is part of the Hello module, no importstatement is necessary.
Run swift build and try out the new version of Hello:
$ swift build
$ .build/debug/Hello `whoami`
To learn about the Swift Package Manager, including how to build modules, import dependencies, and map system libraries, see the Swift Package Manager section of the website.
Sources/ directory called Greeter.swift, and enter the following code:func sayHello(name: String) {
print("Hello, \(name)!")
}
sayHello(name:) function takes a single String argument and prints our “Hello” greeting before, substituting the word “World” with the function argument.main.swift again, and replace the existing contents with the following code:if Process.arguments.count != 2 {
print("Usage: hello NAME")
} else {
let name = Process.arguments[1]
sayHello(name: name)
}
main.swift now reads from the command line arguments. And instead of invoking print(_:) directly, main.swift now calls thesayHello(name:) method. Because the method is part of the Hello module, no importstatement is necessary.swift build and try out the new version of Hello:$ swift build
$ .build/debug/Hello `whoami`
To learn about the Swift Package Manager, including how to build modules, import dependencies, and map system libraries, see the Swift Package Manager section of the website.
Sign up here with your email

ConversionConversion EmoticonEmoticon