Compiling GCC 7.2 on macOS
Introduction
This was my first attempt to have a compatible gcc compiler to compile SystemC and SystemC-AMS on macOS. A better solution is to install the Homebrew package manager and then install the gcc 7.3 package as mentioned here.
Prerequisites
- GCC 4.2 - You need to install xcode version of gcc. Just run "xcode-select --install".
Download all the required packages
Choose one of the GCC mirror servers and download gcc and the required libraries. In my case I have the following setup:
- gmp-6.1.0
- mpfr-3.1.4
- mpc-1.0.3
- isl-0.18
- gcc-7.2.0
Compiling
GMP
cd gmp-6.1.0
mkdir build && cd build
../configure --prefix=/usr/local/gcc-7.2.0 \
--enable-cxx
make -j 4
sudo make install
MPFR
cd mpfr-3.1.4
mkdir build && cd build
cd build/
../configure --prefix=/usr/local/gcc-7.2.0 \
--with-gmp=/usr/local/gcc-7.2.0
make -j4
sudo make install
MPC
cd mpc-1.0.3
mkdir build && cd build
../configure --prefix=/usr/local/gcc-7.2.0 \
--with-gmp=/usr/local/gcc-7.2.0 \
--with-mpfr=/usr/local/gcc-7.2.0
make -j 4
sudo make install
ISL
cd isl-0.18/
mkdir build && cd build
../configure --prefix=/usr/local/gcc-7.2.0 \
--with-gmp-prefix=/usr/local/gcc-7.2.0
make -j4
sudo make install
GCC
cd gcc-7.2.0/
mkdir build && cd build
../configure --prefix=/usr/local/gcc-7.2.0 \
--enable-bootstrap \
--with-tune=generic \
--enable-languages=c,lto,c++,objc,obj-c++,fortran \
--enable-shared \
--enable-static \
--enable-libatomic \
--enable-threads=posix \
--enable-graphite \
--enable-fully-dynamic-string \
--enable-libstdcxx-time=yes \
--disable-libstdcxx-pch \
--disable-libstdcxx-debug \
--disable-isl-version-check \
--enable-lto \
--enable-libgomp \
--disable-multilib \
--enable-checking=release \
--disable-rpath \
--disable-nls \
--disable-werror \
--disable-symvers \
--with-libiconv \
--with-system-zlib \
--with-gmp=/usr/local/gcc-7.2.0 \
--with-mpfr=/usr/local/gcc-7.2.0 \
--with-mpc=/usr/local/gcc-7.2.0 \
--with-isl=/usr/local/gcc-7.2.0 \
--program-suffix=-7.2.0 \
--with-pkgversion='GCC 7.2 for macOS High Sierra' \
--with-gnu-as \
--with-gnu-ld
make -j4
This step will take a long time. I advice you to do other stuff and came back one hour later. Finaly just run:
sudo make install
Setting up the BASH environment
Now we need to tell bash to use our brand new compiler. Otherwise it will keep using the system version. However to maintain compatibility we will not "overwrite" the system compiler executables as we will see bellow.
The first step is to open your ~/.bash_profile and add the following line:
export PATH=/usr/local/gcc-7.2.0/bin:$PATH
The lasiest way to apply the changes is to open a new terminal window. Another way is to run:
source ~/.bash_profile
If everything went well, you should be able to still use the system compiler:
user-desktop:~ user$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
And the new compiler:
user-desktop:~ user$ gcc-7.2.0 -v
Using built-in specs.
COLLECT_GCC=gcc-7.2.0
COLLECT_LTO_WRAPPER=/usr/local/gcc-7.2.0/libexec/gcc/x86_64-apple-darwin17.3.0/7.2.0/lto-wrapper
Target: x86_64-apple-darwin17.3.0
Configured with: ../configure --prefix=/usr/local/gcc-7.2.0 --enable-bootstrap --with-tune=generic --enable-languages=c,lto,c++,objc,obj-c++,fortran --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/usr/local/gcc-7.2.0 --with-mpfr=/usr/local/gcc-7.2.0 --with-mpc=/usr/local/gcc-7.2.0 --with-isl=/usr/local/gcc-7.2.0 --program-suffix=-7.2.0 --with-pkgversion='GCC 7.2 for macOS High Sierra' --with-bugurl=https://americodias.com/docs --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 7.2.0 (GCC 7.2 for macOS High Sierra)