1.安装 Xcode Command Line Tools brew

安装Xcode至最新版本,并安装Command Line Tools

2. brew edit gcc

#找到如下一段内容,并添加patch do部分
class Gcc < Formula
    def arch
        if Hardware::CPU.type == :intel
            if MacOS.prefer_64_bit?
                "x86_64"
            else
                "i686"
            end
        elsif Hardware::CPU.type == :ppc
            if MacOS.prefer_64_bit?
                "powerpc64"
            else
                "powerpc"
            end
        end
    end

    def osmajor
        `uname -r`.chomp
    end

   # 插入如下部分
   # edit by b.nelissen 20140805
   # tobinjones patch gcc https://github.com/Homebrew/homebrew/issues/29845
   patch do
        url "https://gcc.gnu.org/bugzilla/attachment.cgi?id=33180"
        sha1 "def0cb036a255175db86f106e2bb9dd66d19b702"
   end

3. brew install gcc

耗时的编译过程开始了;喝杯茶,等个半小时左右.

编译失败可以尝试执行

rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup 
ruby -e "$(curl -fsSL        https://raw.github.com/Homebrew/homebrew/go/install)" brew install gcc

参考文档1
参考文档2