Home Swift UNIX C Assembly Go Web MCU Research Non-Tech

Swift Command Line Development on macOS: Fix "Undefined Symbols for Architecture x86_64" Linker Error

2025-03-22 | Swift | #Words: 458 | 中文原版

Swift is primarily designed for Apple platforms, but it also supports development on Linux and Windows—particularly excelling for command-line application development.

However, given the small size of the core development team, their focus remains on Apple’s Xcode and GUI development. When working outside of Xcode and GUI environments, various odd issues may arise. I plan to publish several blog posts covering these issues in the coming days.

Linker Error When Building Swift Projects in macOS Command Line

When building a Swift project via the command line on macOS, you may encounter the following linker error:

% swift build
error: 'hello': Invalid manifest (compiled with: ["/Library/Developer/CommandLineTools/usr/bin/swiftc", "-vfsoverlay", "/var/folders/_j/f2g2qcw11379d43mq5rdj5jw0000gn/T/TemporaryDirectory.8ntpr4/vfs.yaml", "-L", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-target", "x86_64-apple-macosx13.0", "-F", "/Library/Developer/CommandLineTools/Library/Developer/Frameworks", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-swift-version", "6", "-I", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-package-description-version", "6.0.0", "/Users/zhonguncle/Desktop/test/swift/hello/Package.swift", "-o", "/var/folders/_j/f2g2qcw11379d43mq5rdj5jw0000gn/T/TemporaryDirectory.X34Ik8/hello-manifest"])
error: link command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture x86_64:
  "PackageDescription.Package.__allocating_init(name: Swift.String, defaultLocalization: PackageDescription.LanguageTag?, platforms: [PackageDescription.SupportedPlatform]?, pkgConfig: Swift.String?, providers: [PackageDescription.SystemPackageProvider]?, products: [PackageDescription.Product], dependencies: [PackageDescription.Package.Dependency], targets: [PackageDescription.Target], swiftLanguageVersions: [PackageDescription.SwiftVersion]?, cLanguageStandard: PackageDescription.CLanguageStandard?, cxxLanguageStandard: PackageDescription.CXXLanguageStandard?) -> PackageDescription.Package", referenced from:
      _main in Package-1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: 'hello': Invalid manifest (compiled with: ["/Library/Developer/CommandLineTools/usr/bin/swiftc", "-vfsoverlay", "/var/folders/_j/f2g2qcw11379d43mq5rdj5jw0000gn/T/TemporaryDirectory.E6a9BS/vfs.yaml", "-L", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-target", "x86_64-apple-macosx13.0", "-F", "/Library/Developer/CommandLineTools/Library/Developer/Frameworks", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-swift-version", "6", "-I", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-package-description-version", "6.0.0", "/Users/zhonguncle/Desktop/test/swift/hello/Package.swift", "-o", "/var/folders/_j/f2g2qcw11379d43mq5rdj5jw0000gn/T/TemporaryDirectory.sX9I7Q/hello-manifest"])
error: link command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture x86_64:
  "PackageDescription.Package.__allocating_init(name: Swift.String, defaultLocalization: PackageDescription.LanguageTag?, platforms: [PackageDescription.SupportedPlatform]?, pkgConfig: Swift.String?, providers: [PackageDescription.SystemPackageProvider]?, products: [PackageDescription.Product], dependencies: [PackageDescription.Package.Dependency], targets: [PackageDescription.Target], swiftLanguageVersions: [PackageDescription.SwiftVersion]?, cLanguageStandard: PackageDescription.CLanguageStandard?, cxxLanguageStandard: PackageDescription.CXXLanguageStandard?) -> PackageDescription.Package", referenced from:
      _main in Package-1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
zhonguncle@ZhongUncle-Mac-mini hello % lipo -i your_private_sdk 
fatal error: /Library/Developer/CommandLineTools/usr/bin/lipo: can't open input file: your_private_sdk (No such file or directory)

Root Cause

This issue likely occurred because I corrupted some system files while uninstalling Xcode and building LLVM/Clang from source. Reinstalling Xcode or installing Clang/Swift via Homebrew did not resolve the problem in this scenario.

Solution

It is recommended to install Xcode (if not already installed), then reset the Xcode command-line toolchain with sudo xcode-select --reset—this will fix the issue:

% swift build              
Fetching https://github.com/apple/swift-argument-parser from cache
Fetching https://github.com/apple/example-package-figlet from cache
Fetched https://github.com/apple/swift-argument-parser from cache (0.03s)
Fetched https://github.com/apple/example-package-figlet from cache (0.03s)
Creating working copy for https://github.com/apple/swift-argument-parser
Working copy of https://github.com/apple/swift-argument-parser resolved at 1.5.0
Creating working copy for https://github.com/apple/example-package-figlet
Working copy of https://github.com/apple/example-package-figlet resolved at main (166eef4)
Building for debugging...
[63/63] Applying MyCLI
Build complete! (24.41s)

I hope this helps anyone who might need it~