Swift 一般是为苹果平台开发的,但是它也可以在 Linux、Windows 上开发,尤其是命令行方面不错。
但是由于团队人不多,一般重心还是在苹果的 Xcode 和 GUI 上,脱离 Xcode 和 GUI 开发的时候,有一些奇奇怪怪的问题,接下来会更新几篇相关博客。
在 macOS 命令行中,使用 Swift 构建一个项目的时候,可能会出现下面这种连接错误:
% 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)
出现可能是我删 Xcode 和构建 LLVM、Clang 的时候把一些东西搞乱了。这种情况下重新安装 Xcode 和使用 HomeBrew 安装 Clang、Swift 也不行。
建议安装 Xcode,再用sudo xcode-select --reset
重置工具链,然后就好了:
% 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)
希望能帮到有需要的人~