Skip to content
Latchkey

swiftc Command: Compile Swift in CI

swiftc compiles Swift source into executables, libraries, or object files.

swiftc is the Swift compiler driver. Most projects build via Swift Package Manager or Xcode, but swiftc appears in scripts and Linux CI images for small tools and cross-platform builds. Its flags cover optimization, target, and SDK selection.

Common flags

  • -o NAME - set the output file name
  • -O - enable optimizations (-Onone to disable)
  • -emit-library / -emit-object - emit a library or object instead of an executable
  • -target ARCH-vendor-os - set the compilation target triple
  • -sdk PATH - path to the SDK (macOS/iOS builds)
  • -Xcc FLAG - pass a flag through to the underlying Clang importer
  • -swift-version 6 - select the Swift language mode

Example in CI

Compile an optimized Swift executable on a Linux CI runner:

shell
swiftc -O -swift-version 6 Sources/main.swift -o app

Common errors in CI

  • error: no such module 'Foundation' - missing SDK or wrong -sdk path
  • error: cannot find 'X' in scope - undeclared identifier or missing import
  • error: value of type 'A' has no member 'b'
  • error: unable to load standard library for target - toolchain/target mismatch

Key takeaways

  • -O optimizes; -target/-sdk are essential for cross/platform builds.
  • On Linux, "no such module Foundation" usually means a missing SDK setup.
  • Use SwiftPM (swift build) for real packages; swiftc is for single-file builds.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →