在 Mac 上安装 GNU GCC [英] Install GNU GCC on mac

查看:36
本文介绍了在 Mac 上安装 GNU GCC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近对 ​​Xcode 5 中包含的新 clang 编译器感到沮丧.我想知道在 OS X 上安装 GNU GCC 的最佳方法是什么.

需要考虑的事项:

  • 我不想使用 MacPorts、fink、homebrew 或任何其他第三方软件包管理器.
  • 如果可能,我想使用从源代码编译的最新 GCC.
  • 我需要现有的 GCC(到 clang 的硬链接)保持默认状态,但在需要时可以轻松使用 GNU GCC.
  • 如果可能,我想避免修改代码.

成功!使用 GCC 4.9.2(使用 GMP 5.1.3、MPFR 3.1.2、MPC 1.0.2、ISL 0.12.2 和 ClooG 0.18.1)我成功地构建了 GCC.从这里获取的提示:

  • 确保您使用 ISL 和 CLoG.是的,它们是可选的,但它们使编译器更加优化,没有它们我就无法构建.确保您使用的是 ISL 0.12,不是最新版本 (0.14).
  • 如果可能,请使用独立的开发人员工具,不要 XCode.(XCode 有一些有问题的标头,虽然我没有注意到构建 GCC 有任何问题,但我在其他软件(例如 GPG)上遇到了问题).
  • 我建议将所有源代码放在 gcc 目录中,而不是单独构建和安装(这样会更快)
  • 确保使用 CC=clang CXX=clang++ 调用 configure 以便 GCC 知道它不是由 GCC 编译的.
  • 绝对,绝对,绝对,使用 -j8 调用 make,否则构建将需要大约 4 个小时!(使用 -j8,我在 2012 年年中使用 8GB RAM 的 MBP 需要 1 1/3 小时.)(make -j8 表示 make可以同时构建 8 个线程(4 核 + HT),而在 2 核机器上,您将运行 make -j4.)

希望这有帮助!

解决方案

我的做法是:

  1. 下载 GCC 的源代码和众多支持包.说明位于 GCC 源代码中的 gcc-4.xy/INSTALL/index.html 文件中,或位于 http://gcc.gnu.org/install/.

  2. 使用脚本将 GCC 的源代码和支持库提取到一个目录中,创建对象目录,然后运行构建.

这是我用于 GCC 4.8.2 的脚本:

GCC_VER=gcc-4.8.2tar -xf ${GCC_VER}.tar.bz2 ||出口 1(cd ${GCC_VER} ||出口猫 <<EOF |克洛格 0.18.0 tar.gzGMP 5.1.3 tar.xzisl 0.11.1 tar.bz2mpc 1.0.1 tar.gzmpfr 3.1.2 tar.xzEOF同时读取文件 vrsn extn做(设置 -xtar -xf "../$file-$vrsn.$extn" &&ln -s "$file-$vrsn" "$file")完毕)mkdir ${GCC_VER}-objcd ${GCC_VER}-obj../${GCC_VER}/configure --prefix=$HOME/gcc/gcc-4.8.2make -j8 引导程序

完成后,也运行安装.然后将 $HOME/gcc/gcc-4.8.2/bin(您在 --prefix 中指定的名称加上 /bin)添加到您的/usr/bin 之前的 PATH.

使用配备 5400 rpm 旋转磁盘的不错的 MacBook Pro,编译所有内容需要一两个小时(使用 -j8 选项make),并且需要编译时需要数 GB 的磁盘空间.SSD 在这样做时很好(绝对更快)!

<小时>

GCC 4.9.0 于 2014-04-22 发布.我已经使用基本相同的过程安装它,但使用 CLoG 0.18.1 和 ISL 0.12.2(需要更新)和 GMP 5.1.3(和 6.0.0a)、MPC 1.0.2(或 1.0.1)和 MPFR3.1.2 在 Mac OS X 10.9.2 Mavericks 和 Ubuntu 12.04 衍生版上.请注意 gmp-6.0.0a.tar.xz 提取到目录 gmp-6.0.0(不是 gmp-6.0.0a 为您可能会期待).

<小时>

在 2014 年和 2017-09-27 之间,我构建了 GCC 版本 4.9.0、4.9.1、5.1.0、5.2.0、5.3.0、6.1.0、6.2.0、6.3.0、7.1.0 与下面显示的 macOS Sierra (10.12) 上的 GCC 7.2.0 的构建脚本只有很小的变化.辅助库的版本变化比较频繁.

<小时>

macOS Sierra 和 High Sierra

在 2017 年 8 月 14 日,我使用上述脚本的一个小变体在 macOS Sierra 10.12 上构建 GCC 7.2.0(使用 XCode 8 作为引导编译器).一个变化是似乎不再需要 CLoG(我停止在 GCC 6.2.0 中添加它).这是我当前的脚本:

#!/bin/bash#export DYLD_LIBRARY_PATH=$(clnpath $(dirname $(dirname $(which g++)))/lib:$DYLD_LIBRARY_PATH)取消设置 DYLD_LIBRARY_PATHTAR=/opt/gnu/bin/tarVER_NUM=7.2.0GCC_VER=gcc-${VER_NUM}TGT_BASE=/opt/gccTGT_DIR=${TGT_BASE}/v${VER_NUM}CC=/usr/bin/clangCXX=/usr/bin/clang++提炼() {echo "提取 $1"$TAR -xf $1}如果 [ !-d "$GCC_VER" ]然后提取 ${GCC_VER}.tar.xz ||出口 1菲(cd ${GCC_VER} ||出口nbncl <<EOF |GMP 6.1.2 tar.lzisl 0.16.1 tar.bz2mpc 1.0.3 tar.gzmpfr 3.1.5 tar.xzEOF同时读取文件 vrsn extn做tarfile="../$file-$vrsn.$extn"如果 [ !-f "$tarfile" ]然后回显找不到 $tarfile">&2;出口1;菲如果 [ !-d "$file-$vrsn" ]然后(设置 -x提取$tarfile"&&ln -s "$file-$vrsn" "$file") ||出口 1菲完毕)如果 [$?= 0 ]然后mkdir ${GCC_VER}-objcd ${GCC_VER}-obj../${GCC_VER}/configure --prefix="${TGT_DIR}" CC="${CC}" CXX="${CXX}"make -j8 引导程序菲

确保您的 tar 版本支持所有 4 种不同的压缩文件格式(.lz.gz.xz, .bz2),但由于 tar 的标准 Mac 版本对我来说是这样,它可能也对你有用.

在 2017 年 9 月 27 日,我未能使用与在 Sierra 10.12 上运行的脚本相同的脚本在 macOS High Sierra 10.13(使用 XCode 9 作为引导编译器)上构建 GCC 7.2.0.直接错误是缺少标头 ;我需要追踪我的 XCode 9 安装是否正确——或者更准确地说,为什么它不正确,因为 是一个 C++98 中的标准头文件.可能有一个简单的解决方法;我只是还没有花时间去追逐它.(是的,我已经多次运行 xcode-select --install;事实上,由于网络故障,我不得不多次运行它,这可能是问题的一部分.)(我让 GCC 7.2.0 在 2017 年 12 月 2 日成功编译;我不记得需要什么体操(如果有的话)才能让它工作.)

时间过去了;版本号增加.但是,基本配方对我来说适用于更新版本的 GCC.我有 7.3.0(2018-01-2 安装)、8.1.0(2018-05-02 安装)、8.2.0(2018-07-26 安装)、8.3.0(2019-03-01 安装)和现在是 9.1.0(今天安装,2019-05-03).这些版本中的每一个都是在当时的 macOS 的当前版本上构建和安装的,在引导阶段使用当前版本的 XCode(因此在构建 GCC 9.1.0 时使用 macOS 10.14.4 Mojave 和 XCode 10.2.1)

I have recently become frustrated with the new clang compiler included with Xcode 5. I was wondering what the best way to install GNU GCC on OS X would be.

Things to consider:

  • I don't want to use MacPorts, fink, homebrew or any other third party package manager.
  • I would like to use the latest GCC, compiled from source, if possible.
  • I need the existing GCC (hardlink to clang) to remain the default, but to easily be able to use GNU GCC when I need to.
  • I would like to avoid modifying the code if at all possible.

EDIT: Success! Using GCC 4.9.2 (with GMP 5.1.3, MPFR 3.1.2, MPC 1.0.2, ISL 0.12.2, and CLooG 0.18.1) I succesfully built GCC. Tips to take from here:

  • Make sure you use ISL and CLooG. Yes, they are optional, but they make a more optimised compiler and I had trouble building without them. Make sure you use ISL 0.12, not the latest version (0.14).
  • If possible, use the standalone Developer Tools, not XCode. (XCode has some buggy headers, and while I didn't notice any issues building GCC I have had issues with other software (e.g. GPG)).
  • I recommend putting all your sources in the gcc directory, rather than building and installing separately (it's faster this way)
  • Make sure you invoke configure with CC=clang CXX=clang++ so GCC knows it isn't being compiled by GCC.
  • Definitely, definitely, definitely, invoke make with -j8, or otherwise the build will take about 4 hours! (With -j8, it took 1 1/3 hours on my Mid-2012 MBP with 8gb RAM.) (make -j8 means make can build 8 threads simultaneously (4 cores + HT), whereas on a 2-core machine you would run make -j4.)

Hope this helps!

解决方案

The way I do it is:

  1. Download the source for GCC and numerous supporting packages. The instructions are in the gcc-4.x.y/INSTALL/index.html file in the GCC source code, or online at http://gcc.gnu.org/install/.

  2. Use a script to extract the source for GCC and the support libraries into a directory, create the object directory, and run the build.

This is the script I used for GCC 4.8.2:

GCC_VER=gcc-4.8.2
tar -xf ${GCC_VER}.tar.bz2 || exit 1

(
cd ${GCC_VER} || exit

cat <<EOF |
    cloog 0.18.0 tar.gz 
    gmp 5.1.3 tar.xz 
    isl 0.11.1 tar.bz2 
    mpc 1.0.1 tar.gz 
    mpfr 3.1.2 tar.xz
EOF

while read file vrsn extn
do
    (
    set -x
    tar -xf "../$file-$vrsn.$extn" &&
    ln -s "$file-$vrsn" "$file"
    )
done
)

mkdir ${GCC_VER}-obj
cd ${GCC_VER}-obj
../${GCC_VER}/configure --prefix=$HOME/gcc/gcc-4.8.2
make -j8 bootstrap

When that finishes, run the install too. Then add $HOME/gcc/gcc-4.8.2/bin (the name you specify in --prefix plus /bin) to your PATH ahead of /usr/bin.

With a decent MacBook Pro with a 5400 rpm spinning disk, it takes an hour or two to compile everything (using the -j8 option to make), and requires multiple gigabytes of disk space while compiling. SSD is nice when doing this (definitely faster)!


GCC 4.9.0 was released on 2014-04-22. I've installed it using basically the same process, but with CLooG 0.18.1 and ISL 0.12.2 (required updates) and GMP 5.1.3 (and 6.0.0a), MPC 1.0.2 (or 1.0.1) and MPFR 3.1.2 on Mac OS X 10.9.2 Mavericks and an Ubuntu 12.04 derivative. Beware that the gmp-6.0.0a.tar.xz extracts into directory gmp-6.0.0 (not gmp-6.0.0a as you might expect).


Between 2014 and 2017-09-27, I've built GCC versions 4.9.0, 4.9.1, 5.1.0, 5.2.0, 5.3.0, 6.1.0, 6.2.0, 6.3.0, 7.1.0 with only minor variations in the build script shown below for GCC 7.2.0 on macOS Sierra (10.12). The versions of the auxilliary libraries changed reasonably often.


macOS Sierra and High Sierra

On 2017-08-14, I used a minor variant of the script above to build GCC 7.2.0 on macOS Sierra 10.12 (using XCode 8 as the bootstrap compiler). One change is that CLooG doesn't seem to be needed any more (I stopped adding it with GCC 6.2.0). This is my current script:

#!/bin/bash

#export DYLD_LIBRARY_PATH=$(clnpath $(dirname $(dirname $(which g++)))/lib:$DYLD_LIBRARY_PATH)
unset DYLD_LIBRARY_PATH

TAR=/opt/gnu/bin/tar
VER_NUM=7.2.0
GCC_VER=gcc-${VER_NUM}
TGT_BASE=/opt/gcc
TGT_DIR=${TGT_BASE}/v${VER_NUM}
CC=/usr/bin/clang
CXX=/usr/bin/clang++

extract() {
    echo "Extract $1"
    $TAR -xf $1
}

if [ ! -d "$GCC_VER" ]
then extract ${GCC_VER}.tar.xz || exit 1
fi

(
cd ${GCC_VER} || exit

nbncl <<EOF |
    gmp     6.1.2   tar.lz 
    isl     0.16.1  tar.bz2 
    mpc     1.0.3   tar.gz 
    mpfr    3.1.5   tar.xz
EOF

while read file vrsn extn
do
    tarfile="../$file-$vrsn.$extn"
    if [ ! -f "$tarfile" ]
    then echo "Cannot find $tarfile" >&2; exit 1;
    fi
    if [ ! -d "$file-$vrsn" ]
    then
        (
        set -x
        extract "$tarfile" &&
        ln -s "$file-$vrsn" "$file"
        ) || exit 1
    fi
done
)

if [ $? = 0 ]
then
    mkdir ${GCC_VER}-obj
    cd ${GCC_VER}-obj
    ../${GCC_VER}/configure --prefix="${TGT_DIR}" 
        CC="${CC}" 
        CXX="${CXX}"
    make -j8 bootstrap
fi

Make sure your version of tar supports all 4 different compressed file formats (.lz, .gz, .xz, .bz2), but since the standard Mac version of tar does that for me, it'll probably work for you too.

On 2017-09-27, I failed to build GCC 7.2.0 on macOS High Sierra 10.13 (using XCode 9 for the bootstrap compiler) using the same script as worked on Sierra 10.12. The immediate error was a missing header <stack>; I'll need to track down whether my XCode 9 installation is correct — or, more accurately, why it isn't correct since <stack> is a standard header in C++98 onwards. There's probably an easy fix; I just haven't spent the time chasing it yet. (Yes, I've run xcode-select --install multiple times; the fact that I had to run it multiple times because of network glitches may be part of the trouble.) (I got GCC 7.2.0 to compile successfully on 2017-12-02; I don't recall what gymnastics — if any — were required to get this to work.)

Time passes; version numbers increase. However, the basic recipe has worked for me with more recent versions of GCC. I have 7.3.0 (installed 2018-01-2), 8.1.0 (installed 2018-05-02), 8.2.0 (installed 2018-07-26), 8.3.0 (installed 2019-03-01) and now 9.1.0 (installed today, 2019-05-03). Each of these versions was built and installed on the current version of macOS at the time, using the current version of XCode for the bootstrap phase (so using macOS 10.14.4 Mojave and XCode 10.2.1 when building GCC 9.1.0)

这篇关于在 Mac 上安装 GNU GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆