如何在OSX上更新G ++编译器

Bri*_*cia 5 c++ macos g++

我尝试使用教程下载g ++的最新版本,并将版本号从4.7更改为最新的(相信是)8.1。但是我收到以下错误

Error: No available formula with the name "gcc81"
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching for similarly named formulae...
==> Searching local taps...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.
Run Code Online (Sandbox Code Playgroud)

有人知道如何更新我的g ++版本吗?这是我尝试查找当前版本时得到的。

g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)

对不起,我真是个菜鸟,我真的想在这里学习。

小智 6

我知道这篇文章有点旧,但我可以就如何实现这一点提供一些启发。

首先,要安装gcc/g++使用 Homebrew,您应该使用上面评论中提到的以下命令:brew install gcc

这样做之后,Homebrew 应该将安装的二进制文件/符号链接放在正确的文件夹中。

为了在 mac 上正确设置gcc/g++命令以使用您刚刚下载的版本,我通过不更改符号链接gcc/g++而是在我的 shell 环境中gccg++在我的 shell 环境中创建别名来做到这一点。

alias gcc='gcc-10'
alias g++='g++-10'
Run Code Online (Sandbox Code Playgroud)

gcc-10g++-10使用 Homebrew 下载。执行此操作时,Homebrew 将gcc-10g++-10放在 /usr/local/bin (位于路径上)中,它允许您为指向 Homebrew 安装的版本的常规gcc/g++命令创建别名。

执行此操作后,运行该命令g++ --version应为您提供以下信息:

g++-10 (Homebrew GCC 10.2.0) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

source ~/.bashrc根据您使用的 shell 类型,您可能需要重新启动终端或运行。