-
Notifications
You must be signed in to change notification settings - Fork 7
GCC Installation on Mac
UPDATED for Mac OS Sonoma 14.4.1, Apple M3 Pro (2024-04-24)
Mac Linux-alike OS doesn't provide upgraded GNU Compliers (GCC), other than LLVM Clang. It says having a gcc and g++ compilers, but actually its Clang (compatible with GCC). Unfortunately and obviously, it doesn't work with FORTRAN code.
Mac default shell NOW is zsh.
It's suggested that nearly most recent stable releases are downloaded from http://ftp.gnu.org.
(NOTE: those are actually needed for building other libraries. So in order to be automatically loaded, it's better to add the bin folder into #PATH and lib folder into #(DY)LD_LIBRARY_PATH (Mac doesn't have this environmental variable anymore, but better have one by doing so in .zshrc).
(1) m4-1.4.19
CC=clang CXX=clang++ ./configure --prefix=/usr/local/autotools
make
sudo make install
(2) autoconf-2.72
CC=clang CXX=clang++ ./configure --prefix=/usr/local/autotools
make
sudo make install
make check
takes a long time to finish with a lot of FAILs. It seems no problem at all.
(3) automake-1.16.5
CC=clang CXX=clang++ ./configure --prefix=/usr/local/autotools
make
sudo make install
make check
takes a long time with a few FAILs.
(4) libtools-2.4.7
CC=clang CXX=clang++ ./configure --prefix=/usr/local/autotools
make
sudo make install
(5) make-4.4.1
(Mac version is 3.8.1@2006)
CC=clang CXX=clang++ ./configure --prefix=/usr/local/autotools
make all
make check
sudo make install
After installing, make a few softlink in /usr/local/bin/, so that 'make', 'gmake' or 'gnumake' is same as should be.
cd /usr/local/bin
sudo ln -sf /usr/local/autotools/bin/make make
sudo ln -sf make gmake
sudo ln -sf make gnumake
(6) wget2-2.1.0 NOTE: this is NOT working anymore on Mac OS (2024-04-23: error messages with OPENSSL). I didn't try it because CURL is working very well on Mac.
CC=clang CXX=clang++ ./configure --prefix=/usr/local --with-openssl=auto --without-libpsl --without-libpcre --without-libpcre2
make all
make check
sudo make install
make check
produces 1 failure relevant to 'iris' remote-connection. It seems no problem.
Since this build is called wget2, either make a solf link in /usr/local/wget -> wget2
, or use it as 'wget2'
WGET is required by GCC to download a few external prerequisites. It may be replaced by CURL but no need to bother it. The above building will not support many function, but open SSL is available.
Since Apple had terminated GCC shipped with its bundle of XCODE or standalone Command Line Tools, it's very hard to build GCC from scratch. The choices include third-pary package installing such MacPort, Homebrew. There are some patches existing around, HERE is one of the best and updating frequently by author (Iains Sandoe):
GCC current-version patched for Darwin OS both Intel(x86-64) and Silicon M (AArch64)
(0) IF NOT YET, have to install/update XCODE Command Line Tools SDK, which provide system compiler (clang/clang++) headers and libs. And usually the package is under: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
(note this is a soft link, pointing to real version current used)
xcode-select --install
(1) prerequisites
After untar the source package, enter the source code folder and download the prerequisites
cd gcc-XXX
./contrib/download_prerequisites
(2)build and install
It's better to do the building and installing outside the source code folder.
mkdir gcc_builddir
cd gcc_builddir
CC=clang CXX=clang++ ../gcc-XXX/configure --prefix=/usr/local/gcc-x/gcc-x-clang --enable-bootstrap --enable-languages=c,c++,fortran --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk --with-gxx-libcxx-include-dir=/usr/include/c++/v1
make all -j10 BOOT_CFLAGS="-O2 -g -Wno-error=deprecated-declarations"
sudo make install
make all
usually takes a long time to finish, but with M3 and j10, it's pretty fast.
make check
doesn't work, if no GNU autogen package.
NOTE on 04-30-2024: OPENMPI has an issue with Fortran LD_FLAGS: -Wl,common_use_dylib, which causes netcdf fortran problem.
For ELM (CLM) and PFLOTRAN, it needs MPI. OPENMPI is a good choice. It MUST be built with GCC from above.
./configure CC=/usr/local/gcc-x/gcc-x-clang/bin/gcc \
CXX=/usr/local/gcc-x/gcc-x-clang/bin/g++ \
FC=/usr/local/gcc-x/gcc-x-clang/bin/gfortran \
CPP=/usr/local/gcc-x/gcc-x-clang/bin/cpp \
CPPFLAGS="-DgFortran -D_LARGE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/local/gcc-x/gcc-x-clang/include " \
LDFLAGS="-L/usr/local/gcc-x/gcc-x-clang/lib " \
--prefix=/usr/local/gcc-x/openmpi-x-gcc/openmpi-5.x --enable-static --enable-shared
NOTE: in the configuring above, \
is the line break sign and should be removed if in one line.
Then build and install, as following:
make all
make check
sudo make install
Alternatively to OPENMPI, MPICH can be built based on GCC from above.
The configuring is:
./configure \
CC=/usr/local/gcc-x/gcc-x-clang/bin/gcc \
CXX=/usr/local/gcc-x/gcc-x-clang/bin/g++ \
FC=/usr/local/gcc-x/gcc-x-clang/bin/gfortran \
F77=/usr/local/gcc-x/gcc-x-clang/bin/gfortran \
CPP=/usr/local/gcc-x/gcc-x-clang/bin/cpp \
CPPFLAGS='-DgFortran -D_LARGE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/local/gcc-x/gcc-x-clang/include' \
CFLAGS='-DgFortran -D_LARGE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/local/gcc-x/gcc-x-clang/include' \
CXXFLAGS='-I/usr/local/gcc-x/gcc-x-clang/include' \
FCFLAGS='-I/usr/local/gcc-x/gcc-x-clang/include' \
FFLAGS='-I/usr/local/gcc-x/gcc-x-clang/include' \
LDFLAGS='-L/usr/local/gcc-x/gcc-x-clang/lib' \
--prefix=/usr/local/gcc-x/mpich-x-gcc/mpich-4.x --enable-static --enable-shared
After successfully configured, do following:
make all
make test
sudo make install