C++ OSX - 用通过 Homebrew 安装的 4.9 替换 gcc 版本 4.2.1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28970935/
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
OSX - replace gcc version 4.2.1 with 4.9 installed via Homebrew
提问by Dacotah
This has been plaguing me for awhile now. I am trying to compile a huge C++ file (I know it works as I it works fine on my Arch Linux computer at work). When I checked my GCC version on my mac It returns the following
这已经困扰我一段时间了。我正在尝试编译一个巨大的 C++ 文件(我知道它可以正常工作,因为它在我的 Arch Linux 计算机上运行良好)。当我在我的 mac 上检查我的 GCC 版本时,它返回以下内容
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
I have also installed the most recent GCC version using Homebrew with
我还使用 Homebrew 安装了最新的 GCC 版本
brew install gcc49
My question now is how do I apply that newly installed GCC version to be the default version that the terminal uses?
我现在的问题是如何将新安装的 GCC 版本应用为终端使用的默认版本?
I am also aware that when you use homebrew to isntall gcc it names it gcc-49 so that there is no confusion between packages.
我也知道当你使用自制软件来安装 gcc 时,它会将它命名为 gcc-49,这样包之间就不会混淆。
I have no idea how to replace the 4.2.1 version that comes with XCode with the 4.9 version I have installed.
我不知道如何用我安装的 4.9 版本替换 XCode 附带的 4.2.1 版本。
Thanks
谢谢
Edit: Switched to my mac to get the full return statement of gcc --version
编辑:切换到我的 mac 以获取 gcc --version 的完整返回语句
Edit 2: My end game here is to be able to navigate to the directory and be able to type
编辑 2:我在这里的最终目标是能够导航到目录并能够输入
make
sudo make install
to install the daemon that has been made. Right now that returns tons of errors with random packages and the Standard Library
安装已经制作的守护进程。现在,随机包和标准库会返回大量错误
回答by Mark Setchell
By default, homebrew
places the executables (binaries) for the packages it installs into /usr/local/bin
- which is a pretty sensible place for binaries installed by local users when you think about it - compared to /bin
which houses standardisded binaries belonging to the core OS. So, your brew
command should have installed gcc-4.9
into /usr/local/bin
. The question is now how to use it... you have several options.
默认情况下,homebrew
将其安装到的软件包的可执行文件(二进制文件)放置在/usr/local/bin
其中 - 当您考虑它时,对于本地用户安装的二进制文件来说,这是一个非常明智的位置 - 与/bin
属于核心操作系统的标准二进制文件相比。因此,您的brew
命令应该已安装gcc-4.9
到/usr/local/bin
. 现在的问题是如何使用它……您有多种选择。
Option 1
选项1
If you just want to compile one or two things today and tomorrow, and then probably not use the compiler again, you may as well just invoke the gcc
installed by homebrew
with the full path like this:
如果你只是想在今天和明天编译一两件事,然后可能不再使用编译器,你也可以像这样使用完整路径调用已gcc
安装的homebrew
:
/usr/local/bin/gcc-4.9 --version
Option 2
选项 2
If you are going to be using gcc
quite a lot, it gets a bit tiresome explicitly typing the full path every time, so you could put the following into your ~/.bash_profile
如果你要使用gcc
很多,每次都明确输入完整路径会有点烦人,所以你可以把以下内容放入你的~/.bash_profile
export PATH=/usr/local/bin:$PATH
and then start a new Terminal and it will know it needs to look in /usr/local/bin
, so you will be able to get away with simply typing
然后启动一个新的终端,它会知道它需要查看/usr/local/bin
,因此您只需输入即可
gcc-4.9 --version
Option 3
选项 3
If you just want to use gcc
to invoke the compiler, without worrying about the actual version, you can do Option 2 above and additionally create a symbolic link like this
如果你只是想用来gcc
调用编译器,而不用担心实际版本,你可以做上面的选项2,另外创建一个像这样的符号链接
cd /usr/local/bin
ln -s gcc-4.9 gcc
That will allow you to run the homebrew
-installed gcc
by simply typing gcc
at the command line, like this
这将允许您通过简单地在命令行输入来运行homebrew
-installed ,就像这样gcc
gcc
gcc --version
Note:
笔记:
If you later want to install, say gcc-4.13
or somesuch, you would do your brew install
as before, then change the symbolic link like this:
如果你以后想安装,说gcc-4.13
或其他,你会brew install
像以前一样做,然后像这样更改符号链接:
cd /usr/local/bin
rm gcc # remove old link from gcc to gcc-4.9
ln -s gcc-4.13 gcc # make new link from gcc to gcc-4.13
回答by eeny
simply updating the order of $PATH in ~/.bash_profile to the brew installed version 'export PATH=/usr/local/Cellar/gcc/5.1.0/bin:$PATH'
was not enough to make the switch for me
简单地将 ~/.bash_profile 中 $PATH 的顺序更新为 brew 安装的版本'export PATH=/usr/local/Cellar/gcc/5.1.0/bin:$PATH'
不足以为我进行切换
changing the alias in your ~./bash_profile (alias gcc='gcc-5'
) works, but can be confusing i.e. which gcc
will return the Clang version
更改 ~./bash_profile ( alias gcc='gcc-5'
) 中的别名有效,但可能会令人困惑,即which gcc
会返回 Clang 版本
what worked for me was to make a symbolic link in the brew gcc directory as well as update the path (point 1 above)
对我有用的是在 brew gcc 目录中创建一个符号链接并更新路径(上面的第 1 点)
cd /usr/local/Cellar/gcc/5.1.0/bin/gcc
ln -s gcc-5 gcc
now which gcc
returns the correct version 5.1.0
现在which gcc
返回正确的版本 5.1.0
回答by Variable Length Coder
OS X does not come with GCC installed (4.2.1 or otherwise). Clang is the default system compiler and has been for some time. It is using the C++ headers from 4.2.1 when invoked as GCC. Have you tried compiling your code with Clang natively, instead of calling "gcc" (which calls Clang)? It has more modern headers and C++ support than the GCC emulation mode.
OS X 未安装 GCC(4.2.1 或其他版本)。Clang 是默认的系统编译器,并且已经有一段时间了。当作为 GCC 调用时,它使用来自 4.2.1 的 C++ 头文件。您是否尝试过使用 Clang 本地编译代码,而不是调用“gcc”(它调用 Clang)?它比 GCC 仿真模式具有更多现代头文件和 C++ 支持。
回答by Pascal Waniha
Download the gcc binaries untar and copy the bin, lib include share and libexec files to your /usr directory then type gcc --version this is what i expect you to see
下载 gcc 二进制文件解压并将 bin、lib include share 和 libexec 文件复制到您的 /usr 目录,然后输入 gcc --version 这是我希望您看到的
gcc --version gcc (GCC) 4.9.2 20141029 (prerelease) Copyright (C) 2014 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.
gcc --version gcc (GCC) 4.9.2 20141029(预发行版) 版权所有 (C) 2014 Free Software Foundation, Inc。这是免费软件;请参阅复制条件的来源。没有保修;甚至不是为了特定目的的适销性或适合性。