xcrun命令

xcrun 是 macOS 上一个非常重要的命令行工具,它属于 Xcode 的一部分,但即使你不安装完整的 Xcode,也可以单独安装 Command Line Tools 来获得它。


xcrun 的主要作用是帮助你查找和执行其他命令行工具,尤其是在这些工具没有直接添加到你的 PATH 环境变量中的情况下。

1.xcrun 的主要功能:

  1. 查找工具: xcrun 能够在系统中搜索特定的命令行工具,即使这些工具不在标准的 PATH 路径下。这对于 Xcode 中包含的许多工具非常有用,因为它们通常位于 Xcode 应用包的深层目录中。

  2. 执行工具: 找到工具后,xcrun 可以直接执行它,而无需你手动输入完整的路径。

  3. 管理 SDK 和工具链: xcrun 还可以帮助你管理不同的 SDK(Software Development Kit)和工具链,例如选择用于编译 iOS 或 macOS 应用程序的特定 SDK。

2.xcrun 的常见用法:

  • 2.1执行工具: 使用 xcrun <tool_name> 可以执行指定的工具。例如,要执行 clang 编译器,你可以使用 xcrun clang。即使 clang 没有在你的 PATH 中,xcrun 也会找到并执行它。

  • 2.2查找工具路径: 使用 xcrun --find <tool_name> 可以查找指定工具的完整路径。例如,xcrun --find clang 会返回 clang 编译器的完整路径。

  • 2.3选择 SDK: 使用 xcrun --sdk <sdk_name> 可以指定要使用的 SDK。例如,xcrun --sdk iphoneos clang 会使用 iOS SDK 中的 clang 编译器。

  • 2.4显示 SDK 路径: 使用 xcrun --show-sdk-path 可以显示当前选择的 SDK 的路径。

  • 2.5显示 SDK 版本: 使用 xcrun --show-sdk-version 可以显示当前选择的 SDK 的版本。

3.xcrun 的重要性:

  • 简化命令行操作: xcrun 极大地简化了在命令行中使用 Xcode 工具的过程,避免了手动查找和输入长路径的麻烦。

  • 支持多版本 Xcode: 如果你安装了多个版本的 Xcode,xcrun 可以帮助你选择要使用的特定版本的工具链。

  • 构建脚本的依赖: 许多构建脚本和自动化工具都依赖于 xcrun 来查找和执行必要的工具。

4.xcrun –help

➜  ~ xcrun --help
Usage: xcrun [options] <tool name> ... arguments ...

Find and execute the named command line tool from the active developer
directory.

The active developer directory can be set using `xcode-select`, or via the
DEVELOPER_DIR environment variable. See the xcrun and xcode-select manual
pages for more information.

Options:
  -h, --help                  show this help message and exit
  --version                   show the xcrun version
  -v, --verbose               show verbose logging output
  --sdk <sdk name>            find the tool for the given SDK name
  --toolchain <name>          find the tool for the given toolchain
  -l, --log                   show commands to be executed (with --run)
  -f, --find                  only find and print the tool path //显示命令原始路径
  -r, --run                   find and execute the tool (the default behavior)
  -n, --no-cache              do not use the lookup cache
  -k, --kill-cache            invalidate all existing cache entries
  --show-sdk-path             show selected SDK install path
  --show-sdk-version          show selected SDK version
  --show-sdk-build-version    show selected SDK build version
  --show-sdk-platform-path    show selected SDK platform path
  --show-sdk-platform-version show selected SDK platform version

5.解决 “xcrun: error: invalid active developer path” 错误:

当你看到 “xcrun: error: invalid active developer path” 错误时,通常意味着系统找不到有效的开发者工具路径。这通常是因为:

  1. 未安装 Command Line Tools 或 Xcode: 你需要安装 Xcode 或单独安装 Command Line Tools。可以通过在终端中运行 xcode-select --install 来安装 Command Line Tools。

  2. Xcode 路径未正确设置: 如果你安装了 Xcode,但仍然看到此错误,可能是 Xcode 的路径未正确设置。你可以使用 sudo xcode-select -switch /Applications/Xcode.app 命令来设置 Xcode 的路径(将 /Applications/Xcode.app 替换为你实际的 Xcode 路径。

⚠️: Gemin汇总