C++ 在 OSX 上更新 GCC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/837992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:30:57  来源:igfitidea点击:

Update GCC on OSX

c++macosgcccompiler-constructiong++

提问by Karan Bhamra

So I am a new programmer and I just installed XCode on my Macbook to get the GCC. I think Xcode is the only way for getting GCC on OSX. Now when I run my Hello World application, in C++, g++ comes up saying it is version 4.0.1 but when I look for commands starting with g I also see g++-4.2. Is there any way of making 4.2 default rather than 4.0.1, and also is there a way to updating gcc to the latest version 4.4.0?

所以我是一个新程序员,我刚刚在我的 Macbook 上安装了 XCode 来获得 GCC。我认为 Xcode 是在 OSX 上获得 GCC 的唯一途径。现在,当我在 C++ 中运行我的 Hello World 应用程序时,g++ 出现说它是 4.0.1 版,但是当我查找以 g 开头的命令时,我也看到了 g++-4.2。有什么方法可以使 4.2 默认而不是 4.0.1,并且有没有办法将 gcc 更新到最新版本 4.4.0?

EDIT: Ok, so I installed macports and installed gcc4.4 and it shows up on terminal as gcc-mp-4.4 and how do I make it default with gcc_select, like what are the commands and stuff. Thanks.

编辑:好的,所以我安装了 macports 并安装了 gcc4.4,它在终端上显示为 gcc-mp-4.4,以及如何使用 gcc_select 使其成为默认值,例如命令和内容。谢谢。

回答by Milhous

If you install macports you can install gcc select, and then choose your gcc version.

如果你安装 macports 你可以安装 gcc select,然后选择你的 gcc 版本。

/opt/local/bin/port install gcc_select

To see your versions use

要查看您的版本,请使用

port select --list gcc

To select a version use

选择版本使用

sudo port select --set gcc gcc40

回答by Briand06

I know it is an old request. But it might still be useful to some. With current versions of MacPorts, you can choose the default gcc version using the port command. To list the available versions of gcc, use:

我知道这是一个古老的请求。但它可能对某些人仍然有用。对于当前版本的 MacPorts,您可以使用 port 命令选择默认的 gcc 版本。要列出 gcc 的可用版本,请使用:

$ sudo port select --list gcc

Available versions for gcc:
gcc42
llvm-gcc42
mp-gcc46
none (active)

$ sudo 端口选择 --list gcc

Available versions for gcc:
gcc42
llvm-gcc42
mp-gcc46
none (active)

To set gcc to the MacPorts version:

将 gcc 设置为 MacPorts 版本:

$ sudo port select --set gcc mp-gcc46

$ sudo 端口选择 --set gcc mp-gcc46

回答by Tom Swirly

I'm just dropping in to say that using a soft link to accomplish this is a terrible, no-good, horrible idea.

我只是顺便说一句,使用软链接来实现这一点是一个可怕的、不好的、可怕的想法。

One of the key things about writing software is reproduceability - you want to be able to get the same results every time. These systems are so complex that you want to reduce all invisible sources of error.

编写软件的关键之一是可复制性——您希望每次都能获得相同的结果。这些系统非常复杂,您希望减少所有不可见的错误来源。

Having a soft link is an invisible source of error. It's the sort of thing you'll forget in a month, then move to a different machine, and wonder why you are getting different results - or, you'll try to upgrade your system, and you'll get weird errors because it's not expecting a softlink there.

拥有软链接是一种无形的错误来源。这是你一个月后会忘记的那种事情,然后转移到另一台机器上,并想知道为什么会得到不同的结果 - 或者,你会尝试升级你的系统,你会得到奇怪的错误,因为它不是期待那里的软链接。

Moreover, this isn't guaranteed to work - in particular, it's not clear that you will get the correct system include files, which have certainly changed between iterations of gcc.

此外,这不能保证工作 - 特别是,不清楚您是否会获得正确的系统包含文件,这些文件在 gcc 的迭代之间肯定会发生变化。

gcc_select is a systematic way of doing the same thing which will work predictably, or in the very worst case you can file a bug report and get an eventual fix or fix it yourself.

gcc_select 是一种系统化的方式来做同样的事情,它可以预测地工作,或者在最坏的情况下,您可以提交错误报告并获得最终修复或自己修复。

Unfortunately :-( gcc_select does not affect which compiler XCode uses so it's not the way to go if you need to work in XCode (which I do). I still don't know what that way might be.

不幸的是:-( gcc_select 不会影响 XCode 使用的编译器,所以如果你需要在 XCode 中工作(我这样做),这不是要走的路。我仍然不知道那可能是什么。

回答by Juan A. Navarro

The following recipe using Homebrewworked for me to update to gcc/g++ 4.7:

以下使用Homebrew 的配方对我有用,可以更新到 gcc/g++ 4.7:

$ brew tap SynthiNet/synthinet
$ brew install gcc47

Found it on a post here.

此处的帖子中找到它。

回答by Juan A. Navarro

use "gcc_select -l"

使用“gcc_select -l”

> gcc_select -l

gcc40 mp-gcc44

> gcc_select mp-gcc44

> gcc_select -l

gcc40 mp-gcc44

> gcc_select mp-gcc44

回答by peter

in /usr/bintype

/usr/bin类型

sudo ln -s -f g++-4.2 g++

sudo ln -s -f g++-4.2 g++

sudo ln -s -f gcc-4.2 gcc

sudo ln -s -f gcc-4.2 gcc

That should do it.

那应该这样做。

回答by stefanB

You can have multiple versions of GCC on your box, to select the one you want to use call it with full path, e.g. instead of g++use full path /usr/bin/g++on command line (depends where your gcc lives).

您可以在您的盒子上安装多个版本的 GCC,以选择您想要使用的版本并使用完整路径调用它,例如,而不是在命令行上g++使用完整路径/usr/bin/g++(取决于您的 gcc 所在的位置)。

For compiling projects it depends what system do you use, I'm not sure about Xcode (I'm happy with default atm) but when you use Makefiles you can set GXX=/usr/bin/g++ and so on.

对于编译项目,它取决于您使用什么系统,我不确定 Xcode(我对默认 atm 感到满意)但是当您使用 Makefiles 时,您可以设置 GXX=/usr/bin/g++ 等等。

EDIT

编辑

There's now a xcrunscript that can be queried to select appropriate version of build tools on mac. Apart from man xcrunI've googled this explanation about xcode and command line toolswhich pretty much summarizes how to use it.

现在有一个xcrun脚本,可以查询在 mac 上选择合适版本的构建工具。除了man xcrun我在谷歌上搜索了关于xcode 和命令行工具的解释之外,它几乎总结了如何使用它。

回答by user702846

You can install your GCC manually

您可以手动安装 GCC

either through

要么通过

sudo port install gcc46

or your download the source code from one of the mirrors from herefor example here

或者您从这里的其中一个镜像下载源代码,例如这里

tar xzvf gcc-4.6.0.tar.gz cd gcc-4.6.0 ./configure make

tar xzvf gcc-4.6.0.tar.gz cd gcc-4.6.0 ./configure make

well if you have multiple version, then through you can choose one

好吧,如果你有多个版本,那么你可以选择一个

port select --list gcc

remember porton mac is called macport https://www.macports.org/install.phpand add add the bin into your path export PATH=$PATH:/opt/local/bin

记住port在 mac 上称为 macporthttps://www.macports.org/install.php并将 bin 添加到您的路径中export PATH=$PATH:/opt/local/bin

回答by Frank Osterfeld

Whatever Apple ships as the default gcc in xcode (4.2.1 on 10.6, 4.0.1 before) is well tested (and maintained) by the apple guys and the "standard" to build software with on OS X. Everything else is not, so think twice if you want to develop software, or be gcc/OS X beta tester.

无论 Apple 作为 xcode 中的默认 gcc 发布(4.2.1 on 10.6,之前的 4.0.1)都经过了苹果人员的良好测试(和维护)以及在 OS X 上构建软件的“标准”。其他一切都不是,所以如果你想开发软件,或者是 gcc/OS X beta 测试者,请三思。