macOS上,Swift脱离Xcode在命令行构建的时候,出现连接错误ld: symbol(s) not found for architecture x86_64

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)

希望能帮到有需要的人~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhonguncle

请我喝一瓶可乐吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值