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 (-Ononeto 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 appCommon 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
-Ooptimizes;-target/-sdkare 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
clang Command: Compile C/C++ in CIclang is the LLVM C/C++ compiler. Reference for -c, -o, -I, -std, -fsanitize, -Weverything, and the diagnosti…
rustc Command: Compile Rust in CIrustc is the Rust compiler. Reference for -O, --edition, --crate-type, -C opt-level, --emit, and the E04xx er…
go build Command: Compile Go in CIgo build compiles Go packages. Reference for -o, -ldflags, -tags, -race, GOOS/GOARCH cross-compilation, and t…