即使安装了命令行工具,xcode-select 也无法找到 clang

当我尝试运行 clang 命令时,出现以下错误:

xcode-select: Failed to locate 'clang', requesting installation of command line developer tools.

然后我收到这个弹出窗口:

clang installation popup

但是,无论我单击“安装”多少次(经过 15 分钟的安装过程),我都会继续收到相同的错误。

我也尝试过运行 xcode-select --install 但是会出现以下错误:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

我也尝试过从 here 手动安装。

我在运行 macOS Monterey 版本 12.5 的 M1 mac 上。

它还会产生一个核心转储:

2022-09-19 10:51:04.669 xcodebuild[1541:17443] [MT] DVTPlugInLoading: Failed to load code for plug-in com.apple.dt.IDESimulatorAvailability (/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin), error = Error Domain=NSCocoaErrorDomain Code=3588 "dlopen(/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability, 0x0109): Symbol not found: (_OBJC_CLASS_$_SimDiskImage)
  Referenced from: '/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability'
  Expected in: '/Library/Developer/PrivateFrameworks/CoreSimulator.framework/Versions/A/CoreSimulator'" UserInfo={NSLocalizedFailureReason=The bundle couldn’t be loaded., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability, NSDebugDescription=dlopen(/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability, 0x0109): Symbol not found: (_OBJC_CLASS_$_SimDiskImage)
  Referenced from: '/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability'
  Expected in: '/Library/Developer/PrivateFrameworks/CoreSimulator.framework/Versions/A/CoreSimulator', NSBundlePath=/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin, NSLocalizedDescription=The bundle “IDESimulatorAvailability” couldn’t be loaded.}, dyldError = dlopen(/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability, 0x0000): Symbol not found: (_OBJC_CLASS_$_SimDiskImage)
  Referenced from: '/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability'
  Expected in: '/Library/Developer/PrivateFrameworks/CoreSimulator.framework/Versions/A/CoreSimulator'
2022-09-19 10:51:04.688 xcodebuild[1541:17443] [MT] DVTAssertions: ASSERTION FAILURE in /System/Volumes/Data/SWE/Apps/DT/BuildRoots/BuildRoot2/ActiveBuildRoot/Library/Caches/com.apple.xbs/Sources/DVTFrameworks/DVTFrameworks-21303/DVTFoundation/PlugInArchitecture/DataModel/DVTPlugIn.m:374
Details:  Failed to load code for plug-in com.apple.dt.IDESimulatorAvailability (/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin)
Please ensure Xcode packages are up-to-date — try running 'xcodebuild -runFirstLaunch'.

NSBundle error: Error Domain=NSCocoaErrorDomain Code=3588 "dlopen(/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability, 0x0109): Symbol not found: (_OBJC_CLASS_$_SimDiskImage)
  Referenced from: '/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability'
  Expected in: '/Library/Developer/PrivateFrameworks/CoreSimulator.framework/Versions/A/CoreSimulator'" UserInfo={NSLocalizedFailureReason=The bundle couldn’t be loaded., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability, NSDebugDescription=dlopen(/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability, 0x0109): Symbol not found: (_OBJC_CLASS_$_SimDiskImage)
  Referenced from: '/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin/Contents/MacOS/IDESimulatorAvailability'
  Expected in: '/Library/Developer/PrivateFrameworks/CoreSimulator.framework/Versions/A/CoreSimulator', NSBundlePath=/Applications/Xcode.app/Contents/PlugIns/IDESimulatorAvailability.ideplugin, NSLocalizedDescription=The bundle “IDESimulatorAvailability” couldn’t be loaded.}
Object:   <DVTPlugIn: 0x60000080b7a0>
Method:   -loadAssertingOnError:error:
Thread:   <_NSMainThread: 0x600002c402c0>{number = 1, name = main}
Hints:

Backtrace:
  0  0x000000010653d410
  1  0x000000010653caec
  2  0x000000010653cc6c
  3  0x00000001063eb2ac
  4  0x00000001063b1118
  5  0x00000001063af528
  6  0x0000000181aa01b4
  7  0x0000000181aaf414
  8  0x000000010658158c
  9  0x000000010655d4b0
 10  0x00000001063af3c0
 11  0x00000001063af5e0
 12  0x00000001074372ec
 13  0x0000000107436ae0
 14  0x00000001074361e8
 15  0x0000000104c70324
 16  0x0000000104c21e0c
sh: line 1:  1543 Abort trap: 6           /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -find clang 2> /dev/null
clang: error: sh -c '/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -find clang 2> /dev/null' failed with exit code 34304: (null) (errno=Invalid argument)

如何安装clang?

stack overflow xcode-select cannot locate clang, even after installing command line tools
原文答案

答案:

作者头像

请确保 Xcode 包是最新的——尝试运行 xcodebuild -runFirstLaunch

这对我有用。

作者头像

我遇到了同样的问题并更新了 Xcode、重新链接路径等,但没有帮助。

有效的是在更新后打开 Xcode 并接受提示以加载必要的开发工具(用于 macOS 和 iOS)。

隐藏在错误消息中的还有 CLI 解决方案:“请确保 Xcode 包是最新的 - 尝试运行 'xcodebuild -runFirstLaunch'。”

作者头像

您也可以打开 Xcode,选择默认的运行时工具(在我的例子中是 MacOS 和 iOS),然后 clang 应该正常运行。

我相信这与@LanderTome 的回答相同,但无法验证。

作者头像

在 MacOS Ventura 13.0.1 上安装 XCode 14.1 后,我的 clang 没有工作,出现与此非常相似的错误,并反复尝试安装 XCode 命令行工具。事实证明, xcode-select 指向作为完整 XCode 包一部分的命令行工具,但未能在其中找到 clangclang 包含在命令行工具包中,但不包含在某些版本的完整 XCode 包中。

我所做的修复是:

sudo xcode-select -switch /Library/Developer/CommandLineTools

这会切换 clang 和其他工具运行的位置。如果你想切换回从 XCode 包运行它们,你可以使用:

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer