0Pricing
Swift Academy · Lesson

Library vs executable packages

Understand SwiftPM library vs executable packages: how to declare products/targets, consume libraries, and run executables.

Two product types

SwiftPM packages can ship as a library (code to import) or an executable (a runnable tool).

  • Declare products in Package.swift
  • Organize targets and sources
  • Consume libraries; run executables

Library product basics

A library exposes modules that others can import. No entry point is required.

// Package.swift (illustrative library)
// import PackageDescription
// let package = Package(
//   name: "GreeterLib",
//   products: [
//     .library(name: "GreeterLib", targets: ["GreeterLib"]),
//   ],
//   targets: [
//     .target(name: "GreeterLib"),
//     .testTarget(name: "GreeterLibTests", dependencies: ["GreeterLib"]),
//   ]
// )
//
// Sources/GreeterLib/Greeter.swift
public struct Greeter {
    public init() {}
    public func hello(_ name: String) -> String { "Hello, \\(name)!" }
}
print(Greeter().hello("Ana")) // demo run

All lessons in this course

  1. Library vs executable packages
  2. Semantic versioning & tagging releases
  3. CI basics with swift test and artifacts
← Back to Swift Academy