Compile GCC from source on MacOS 12 (Monterey x86_64)

OK since MacOS 11, I've have never had much luck compiling stuff using the native Xcode compiler. I think this is due to the fact that Apple is using clang instead of gcc as the default compiler AND the fact the apple removed most all include files out of /usr/include and libraries out of /usr/lib and put these into a dynamic cache to speed up booting.

see: https://superuser.com/questions/1488979/macos-catalina-cannot-find-usr-include-file

As a result, I pretty much could not get anything to compile on Big Sur (11) or Monterey (12) using the standard compiler.

This really bothered me as I want to be able to compile my own apps (such as PHP, Apache, Mysql, or even Nethack/Slashem)

Now, I know what you are thinking... "John then install brew or MacPorts ... they setup a complete dev environment for you!" That's true, but the install ends up dumping like 5GB of files on your machine. That just seems a bit excessive ... in my opinion. The stuff I want... apache httpd, openSSL, php, mysql, nethack, is only like 1G tops.

The last version of MacOS with more/less a working dev environment (i.e. having complete files under /usr/include and /usr/lib) was OSX Mojave...

When I compiled executables on that platform .. .they always seemed to run on Big Sur and Monterey...

So this page documents my effort to compile gcc 11.3 on OSX Mojave and then to move it to MacOS 12 Monterey on the intel x86_64 platform.

If you don't feel like compiling this, and are ok with downloading and running unsigned code as root on your mac you can click the below links to just download the compiled executables.

BE SURE TO TAKE A BACKUP OF YOUR SYSTEM BEFORE TRYING AND THAT YOU UNDERSTAND THAT WE ARE NOT RESPONSIBLE FOR ANY PROBLEMS THIS MAY CAUSE ON YOUR SYSTEM(S)

Please note that you WILL need to setup a login to do so. But it's free!
  • gcc11.3-darwin-x86_64.tgz
  • gmp6.2.1-darwin-x86_64.tgz
  • mpc1.3.1-darwin-x86_64.tgz
  • mpfr4.2.0-darwin-x86_64.tgz

NOTE: If you want to just download the files you will need to grab all (4) tgz, then extract into /usr/local/ as root. Then go BELOW to setup the appropriate environment variables.

Otherwise to build yourself ... proceed...

This is done on the following:

Create a VMware virtual machine, then boot from the DMG and install OSX 10.14 onto the VM.

Perform the following on the VM Mojave Server:

Login to the VM, open a terminal, then type gcc. This will install the xcode command line tools.

Once you are done. Verify the command line tools are installed by running the following:

$ ls -l /Library/Developer/CommandLineTools/SDKs/

sh-3.2# ls -l /Library/Developer/CommandLineTools/
total 0
drwxr-xr-x  5 root  admin  160 Jul 12  2019 Library
drwxr-xr-x  3 root  admin   96 Jul 12  2019 Packages
drwxr-xr-x  4 root  wheel  128 Mar 17 06:57 SDKs
drwxr-xr-x  7 root  admin  224 Jul 12  2019 usr
sh-3.2#

 

Extract all files to some install directory at the save level. For xz files use tar -Jxf [filename]. I used the directory ./apps

$ ls -l apps
total 0
drwxr-xr-x@  84 jchung  staff   2688 Apr 21  2022 gcc-11.3.0
drwxr-xr-x@ 130 jchung  staff   4160 Mar 14 18:56 gmp-6.2.1
drwxr-xr-x@  27 jchung  staff    864 Mar 14 19:08 mpc-1.3.1
drwxr-xr-x@  42 jchung  staff   1344 Mar 14 19:00 mpfr-4.2.0

Build all required apps:

  • (GMP)
  • cd gmp-6.2.1
  • $ ./configure --prefix=/usr/local/gmp
  • make
  • make install (as root)
  • cd ..
  • (MPFR)
  • cd mpfr-4.2.0
  • $ ./configure --prefix=/usr/local/mpfr --with-gmp=/usr/local/gmp
  • make
  • make install (as root)
  • cd ..
  • (MPC)
  • $ ./configure --prefix=/usr/local/mpc --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr
  • make
  • make install (as root)
  • cd ..
  • We are now ready to build gcc
  • create a directory call gcc_obj at the same level of the gcc-11.3.0 directory
  • cd gcc_obj
  • ls -l
  • $ ls -l apps
    total 0
    drwxr-xr-x@  84 jchung  staff   2688 Apr 21  2022 gcc-11.3.0

    drwxr-xr-x@  84 jchung  staff   2688 Apr 21  2022 gcc_obj
  • drwxr-xr-x@ 130 jchung  staff   4160 Mar 14 18:56 gmp-6.2.1
    drwxr-xr-x@ 535 jchung  staff  17120 Mar 14 19:21 isl-0.24
    drwxr-xr-x@  27 jchung  staff    864 Mar 14 19:08 mpc-1.3.1
    drwxr-xr-x@  42 jchung  staff   1344 Mar 14 19:00 mpfr-4.2.0
  • cd gcc_obj
  • ../gcc-11.3.0/configure --prefix=/usr/local/gcc11 --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr --with-mpc=/usr/local/mpc --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
  • make -j 2
    now wait for about 2 hours
  • make
  • make install (as root)

At this point all of the apps should be under /usr/local

Johns-MBP:~ jchung$ ls -l /usr/local
total 0

drwxr-xr-x    7 root  wheel   224 Mar 18 06:42 gcc11
drwxr-xr-x    5 root  wheel   160 Mar 17 18:20 gmp
drwxr-xr-x    5 root  wheel   160 Mar 17 18:32 mpc
drwxr-xr-x    5 root  wheel   160 Mar 17 18:30 mpfr

POST INSTALL

Note that you need to do following if you compile OR if you just downloaded the TGZ files from above
  • Tar these directories on the VM and then transfer them to the host machine under the same path. /usr/local is the only directory under /usr that isn't controlled by SIP and is writable. VMWare Fusion does have networking between host and guest so you should be able to do this using sftp.
  • At this point you are done with the VM.
  • Go back to the host machine (running Big Sur or Monterey).
  • set the following environment variables in .bash_profile
  • export LDFLAGS="-L/usr/local/gcc11/lib $LDFLAGS"
    export CFLAGS="-I/usr/local/gcc11/lib/gcc/x86_64-apple-darwin18.7.0/11.3.0/include"
  • export PATH=/usr/local/gcc11/bin:$PATH

Now on the host, source .bash_profile and test compile HELLO WORLD.

$ cat test.c
#include
main() {
 printf("hello world\n");
}

$ which gcc
/usr/local/gcc11/bin/gcc
$ gcc test.c
test.c:2:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    2 | main() {
      | ^~~~
$ ./a.out
hello world

At this point, you are done. Using this, I was pretty much able to manually compile all of the apps that I needed.