/ __/ __  ___ ___  ___ ___
  _\ \/ _ \/ -_) _ `/ __/ -_)
 /___/ .__/\__/\_,_/_/  \__/
Speare Pro v1.2.76
The free small IDE for scripting languages.
(c) 2020 http://sevenuc.com

C and C++ programming on macOS

Speare code editor is an ultra lightweight code editor and a small IDE that has an efficient code navigation and call routines tracing ability, and has an integrated debugging environment for C and C++, it well support C and C++ programming on macOS, any type of C and C++ project, from embedded system to desktop application and sever side projects.

How to compile and install GCC on macOS?

If some reason force you must install GCC on your macOS to provide an alternative C and C++ compiler instead of Clang comes with Xcode, this page described how to do it step by step in details. The instructions on this page were tested on High Sierra and they should works on Mojave and Catalina.

Building GCC 9 from sources could take some time, in my case it took about two hours on my MacBook Pro. In order to compile GCC from sources you will need a working C++ compiler. In the remaining of this page I will assume that you have installed the Command Line Tools for Xcode. At the time of this writing Apple’s Command Line Tools maps the gcc and g++ to clang and clang++. If you don’t have the Command Line Tools installed, open a Terminal and execute the following command:

xcode-select --install

Now, let’s start by creating a working folder:

cd ~
mkdir gcc && cd gcc
Next, download and extract a stable version of GCC:
curl -O https://ftpmirror.gnu.org/gcc/gcc-9.1.0/gcc-9.1.0.tar.xz
curl -O ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
curl -O ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
curl -O ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
curl -O ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2
We will start by compiling and installing the gmp library:
tar -xvf gmp-6.1.0.tar.bz2
cd gmp-6.1.0
./configure --prefix=~/gcc --enable-cxx
make install
We will do the same steps for mpfr now:
cd ..
tar -xvf mpfr-3.1.4.tar.bz2
cd mpfr-3.1.4
../configure --prefix=~/gcc --with-gmp=~/gcc
make install
Now, we are going to build mpc:
cd ..
tar -xvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --prefix=~/gcc \
  --with-gmp=~/gcc \
make install
Next step is to build the library for the Graphite loop optimizations:
cd ..
tar -xvf isl-0.18.tar.bz2
cd isl-0.18
./configure --prefix=~/gcc --with-gmp-prefix=~/gcc
make install
After above steps, we are ready to compile GCC now.
cd ..
tar -xvf gcc-9.1.0.tar.xz
cd gcc-9.1.0
./configure --prefix=~/gcc \
  --enable-checking=release \
  --with-gmp=~/gcc \
  --with-mpfr=~/gcc \
  --with-mpc=~/gcc \
  --enable-languages=c,c++ \
  --with-isl=~/gcc \
If you are interested in building more compilers available in the GCC collection just modify the –enable-languages configure option, e.g --enable-languages=c,c++,fortran to add Fortran compiler.
And now, execute the commands:
This step may take approximately, depending on your computer configuration, an hour or more, and about 5.82GB of your disk space for the building.

When the compile finished, install the compiled gcc in ~/gcc:
make install
Now, you can use the new compiled gcc by export the PATH environment variable:
export PATH=~/gcc/bin:$PATH
If you want to avoid writing the above command each time you open a Terminal, save the above command in the file .bash_profile from your Home folder, e.g:
echo 'export PATH=~/gcc/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile
In your Makefile, use the new compiled gcc-9.1 or g++-9.1 and set the include path:
CC = g++-9.1
CXXFLAGS += -std=c++11 -I~/gcc/include/c++/9.1.0
After above settings, you can develop C and C++ with a stable version of GCC that compiled by yourself now.