How do I install GCC via Homebrew?

4 Answers

up vote 6 down vote accepted

The solution provided by @Konrad Rudolph is not entirely correct anymore as the GCC formula that he mentioned was moved from homebrew/dupes to homebrew/versions. You can choose which version of GCC to install. For example, at the time of writing this answer, version 4.5, 4.7 and 4.8 are available. You may check out what versions are available here.

In short, you can install GCC 4.8 by using

brew tap homebrew/versions
brew install [flags] gcc48
share improve this answer
 
I tried this and I got it to install but it dies like this: gcc-4.8 hello.c gcc-4.8: error trying to exec 'as': execvp: No such file or directory –  Warren P  Mar 27 at 21:36
 
@WarrenP Did you install with any [flags]? On my machines, I installed without any flags and it works fine. Why not you try to call gcc-4.8 without any input file and see if you get a fatal error? Also try to ls /usr/local/Cellar/gcc48/4.8.0/bin and see if that directory contains gcc-4.8. –  yihangho  Mar 28 at 9:28
 
It does contain gcc-4.8 and gcc-4.8 --version runs but it can't exec out to the secondary tool named as and so it dies. –  Warren P  Mar 28 at 12:19
 
Hmm... Frankly I have no idea how to solve this. Maybe we have to wait for some more experienced user... –  yihangho  Mar 29 at 0:57
 
How to add g++ as a command in my terminal? do I have to soft link/usr/local/Cellar/gcc48/4.8.1/bin/g++48 in my /usr/bin? –  nkint  Jun 7 at 14:32
show 1 more comment

Homebrew solution

To answer my own question, homebrew-versions now has a fairly up to date formula of GCC. It can be installed using

brew install [flags] https://raw.github.com/Homebrew/homebrew-versions/gcc48.rb

Where [flags] should include all the required languages, e.g. (--enable-cxx --enable-fortran).

This will install the executables with a suffix, i.e. gcc has to be accessed as gcc-version to avoid clashes. If necessary, one can create appropriate symlinks to make this version the default.

Manual installation

Alternatively, an up-to-date GCC (as of the time of writing) can be compiled manually using the following shell script:

VERSION=4.7.0
PREFIX=/usr/gcc-$(VERSION)
LANGUAGES=c,c++,fortran
MAKE=make
# Or
# MAKE='make -j 4' # to compile using four cores

brew-path() { brew info $1 | head -n3 | tail -n1 | cut -d' ' -f1; }

# Prerequisites

brew install gmp
brew install mpfr
brew install libmpc

# Download & install the latest GCC

mkdir -p $PREFIX
mkdir temp-gcc
cd temp-gcc
wget ftp://ftp.gnu.org/gnu/gcc/gcc-$VERSION/gcc-$VERSION.tar.gz
tar xfz gcc-$VERSION.tar.gz
rm gcc-$VERSION.tar.gz
cd gcc-$VERSION

mkdir build
cd build

../configure \
   --prefix=$PREFIX \
   --with-gmp=$(brew-path gmp) \
   --with-mpfr=$(brew-path mpfr) \
   --with-mpc=$(brew-path libmpc) \
   --program-suffix=-$VERSION \
   --enable-languages=$LANGUAGES \
   --with-system-zlib \
   --enable-stage1-checking \
   --enable-plugin \
   --enable-lto \
   --disable-multilib

$MAKE bootstrap

make install

# Uncomment for cleanup …
# cd ../../..
# rm -r temp-gcc

This will stage GCC into the path /usr/gcc-4.7.0. Now all you need to do is either create symlinks to the executables or add the bin directory to the $PATH variable.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值