将默认 GCC 设置为 Xcode 命令行工具

Set the default GCC to Xcode Command Line Tools

如何确保 gcc 指向特定的编译器?我可以为 gccxcode 版本创建永久符号 link 吗?

我的系统上安装了多个版本的 gcc 编译器,包括

`gcc-11` found at `/usr/local/gcc-10/share` (compiled from source following these [instructions][1])
`gcc-9.3.0` found at `/usr/local/Cellar/gcc/9.3.0_1/share`
`gcc-4.8.5` found at `/Users/PatrickT/miniconda/pkgs/gcc-4.8.5-8/share`

默认版本为gcc-4:

gcc --version
gcc (GCC) 4.8.5
which gcc
/Users/PatrickT/miniconda/bin/gcc

这个 gcc 的 miniconda 版本不适合我。如果我从 PATH 中删除 miniconda,我的系统将恢复到另一个版本的 gcc 并且一切都按预期工作。但是,我确实使用了 miniconda 的 Python,因此我想将它保留在我的 PATH 中。在我的 .zhrc 配置文件中(显然,MacOS Catalina 已将 .bashrc 移至 .zhrc),我有:

export PATH="/Users/PatrickT/miniconda/bin:$PATH"
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/Cellar/gcc:$PATH"
export PATH="/usr/local/gcc-10/bin:$PATH"

我试图改变这些行的顺序,但没有任何区别:miniconda 的 gcc 仍然是默认编译器,除非我完全删除第一行,但是......我是无法使用 conda 的 Python!

背景:我没有直接使用 gcc,但我似乎需要它来编译某些脚本。如果重要的话,我同时拥有 xcode 命令行工具和 xcode 应用程序。我在 MacOS Catalina 10.15.4

在出现更好的解决方案之前,您可以使用

删除 Anaconda 的 gcc
    conda remove gcc

并检查您的系统现在是否正在从 Xcode

    gcc --version
    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
    Apple clang version 11.0.3 (clang-1103.0.32.59)
    Target: x86_64-apple-darwin19.4.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

在有知识的人回答这个问题之前,这里有一些思考:

  • cc是一个符号link到gcc

  • CC是引用系统C编译器的环境变量。

  • gcc 是编译器的符号 link。

  • 典型的默认值是 CC=cc,其中 cc 通常是 link 到 gcc 的符号。

  • 您可以通过设置 CC?=gccCC=gcc 来更改特定任务的调用。 ?=' operator is a conditional variable assignment operator: will have an effect only if the variableCCis not yet defined. The=operator will supersede any previously set link. OrCC=gcc-10` 调用系统上安装的特定版本。

  • CC 可以是 link 到 clang (MacOS) 或 g++ (Linux)。请参阅相关问题的 this answer,特别是 Jonathan Leffler 的回答和 Josh Kodroff 的评论。

参考文献:GNU Manual on Implicit Variables, GNU Manual on Make.