在 macOS Catalina(以及之前的版本,很可能还有后续版本)上,存在两个方面的问题和一些建议的解决方案。
make默认使用的编译器名称是什么?
$ mkdir junk
$ cd junk
$ > x.cpp
$ > y.c
$ make x y
c++ x.cpp -o x
cc y.c -o y
$ cd ..
$ rm -fr junk
这表明make使用的名称是cc和c++。显然不是clang 或clang++,但也不是很明显gcc 和g++。
$ which cc c++
/usr/bin/cc
/usr/bin/c++
$
究竟是哪个编译器?
哪个编译器真正存在于名称cc、c++、gcc、g++、clang 和clang++ 后面?我们可以通过让它们识别它们的版本来检查它们到底是哪个编译器:
$ for compiler in cc c++ gcc g++ clang clang++
> do
> which $compiler
> $compiler --version
> done
/usr/bin/cc
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/c++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/gcc
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/g++
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$
如您所见,/usr/bin中安装的版本都是同一个编译器,那个编译器是clang或clang++。
这是在装有 macOS Mojave 10.14.6 和 XCode 11.3.1 的机器上运行的。最新版本的 XCode — 11.4.1 — 仅在 Catalina 上可用。但是,总体结论是一样的——所有的 C 和 C++ 编译器实际上都是 clang 和 clang++ 伪装的。
如何将 GNU GCC 安装到您的机器上?
如何在您的机器上安装一个真正的 GNU GCC——一个真正的 GCC,而不是伪装的 clang?
- 使用Brew 安装 GCC(我没有检查当前的 GCC 版本)。
- 或者使用MacPorts(同样,我没有检查当前的 GCC 版本)。
- 如果你喜欢冒险,那就自己动手吧(但我还没有成功地在 Catalina 上构建 GCC 9.3.0;我有一个基于 macOS Mojave 10.14.x 构建的 GCC 9.2.0,但它在 Catalina 上运行良好 —需要一个环境变量来定位标头)。
- 可能是Fink——它列出了 GCC 8.4 将于 2020 年推出;我不知道新版本。
请注意,Apple 已将系统头文件隐藏在很远的地方(不在 /usr/include 中 - 您无法修改文件系统的该部分以将符号链接添加到它们隐藏它们的位置):
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
(你的意思是你猜不到?我也猜不到!)
如何更改默认编译器?
在适当的地方安装 GCC 后,您需要确保使用“真正的”GCC 而不是 /usr/bin 中的“假”。您可以通过确保“真正的”GCC 的 bin 目录出现在您的 PATH 上 /usr/bin 之前的部分来做到这一点。我在/opt/gcc/v9.3.0 下安装了GCC 9.3.0,所以/opt/gcc/v9.3.0/bin 早在/usr/bin 出现在我的PATH 上。
您还需要确保riak(您要安装的软件)的配置使用正确的编译器。如果有 ./configure 脚本,请使用为编译器指定的正确路径运行它。例如,我可能会使用:
./configure CC=/opt/gcc/v9.3.0/bin/gcc CXX=/opt/gcc/v9.3.0/bin/g++
您也可以将这些值设置为环境变量。
如果它使用cmake 或其他一些配置包,您需要查阅安装说明。这通常是 README 或有时是 INSTALL。
另见(越来越旧的帖子):