0Pricing
Swift Academy · Lesson

Library vs executable packages

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

Library vs executable packages is a free Swift Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Executable product basics

An executable has an entry point (main.swift) and builds a runnable binary (swift run).

// Package.swift (illustrative executable)
// import PackageDescription
// let package = Package(
//   name: "greeter",
//   products: [ .executable(name: "greeter", targets: ["App"]) ],
//   targets: [ .executableTarget(name: "App") ]
// )
//
// Sources/App/main.swift
let args = CommandLine.arguments.dropFirst()
let who = args.first ?? "world"
print("Hello, \\(who)")

Depending on libraries

Use .package and .product in the consumer to import a library. Pin with semantic versions (e.g., from: "1.0.0").

// In the consumer's Package.swift (illustrative):
// dependencies: [
//   .package(url: "https://example.com/GreeterLib.git", from: "1.0.0")
// ],
// targets: [
//   .executableTarget(
//     name: "App",
//     dependencies: [ .product(name: "GreeterLib", package: "GreeterLib") ]
//   )
// ]
//
// Sources/App/main.swift
// import GreeterLib
struct Dummy {}
print("Add import GreeterLib in your app to use its APIs.")

Split responsibilities

Share code: put core logic in a library, and keep the executable as a thin wrapper.

// Package.swift can expose both a library and an executable:
// products: [
//   .library(name: "GreeterLib", targets: ["GreeterLib"]),
//   .executable(name: "greeter", targets: ["GreeterCLI"]),
// ],
// targets: [
//   .target(name: "GreeterLib"),
//   .executableTarget(name: "GreeterCLI", dependencies: ["GreeterLib"]),
// ]
//
// Reuse logic in the library; keep CLI thin.
print("Tip: put reusable logic in the library; the CLI depends on it.")

Build/run commands

Build libraries with swift build and test with swift test. Run executables via swift run <name>.

// Library: build & test
//   swift build
//   swift test
//
// Executable: run & release
//   swift run greeter Ana
//   swift build -c release
//
// Artifact:
//   .build/release/greeter
print("Use swift run for executables; import libraries into apps.")

Library vs executable definition

Quick check: What distinguishes a library from an executable in SwiftPM?

Recap

Recap: Use a library to share code across apps and an executable to ship a CLI/tool. Declare products and targets in Package.swift, and depend on libraries via .package + .product.

Frequently asked questions

Is the “Library vs executable packages” lesson free?

Yes — the full text of “Library vs executable packages” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Library vs executable packages”?

Understand SwiftPM library vs executable packages: how to declare products/targets, consume libraries, and run executables. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Library vs executable packages” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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