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. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.
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 runExecutable 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.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Library vs executable packages” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Library vs executable packages”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
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
- Library vs executable packages
- Semantic versioning & tagging releases
- CI basics with swift test and artifacts