macOS Users: How to Switch to GNU Compiler Collection

I never grew up with a Mac in the house. Because I was most comfortable with Windows machines, upon entering university I was gifted a Windows laptop by my parents. Given that some of my computer science courses graded our programs on lab machines that ran a Linux distribution, I soon installed Ubuntu as a dual-boot on my laptop to gain more familiarity and experience with the operating system. That meant I spent much of my time writing C and C++ programs in a Linux environment before my professional career. As I joined Atomic Object, I began to use a MacBook Pro, and quickly realized I wish I had the GNU Compiler Collection (GCC) that I was familiar with from school, instead of Clang (the default macOS C/C++ compiler).

This post will show new macOS users with a Linux background how to switch to using GNU Compiler Collection instead of the Clang compiler, and benefit from the familiarity and additional features of GCC.

Setting Up GNU Compiler Collection

To begin, we will need to install Homebrew. Navigate to brew.sh, copy their install command, and paste it into your macOS terminal and follow the instructions. We need Homebrew to allow us to install GCC. Next, from your terminal, run brew install gcc. After installation finishes, run gcc in the terminal. You will get the following:

GCC showing a Clang error message

But if you run gcc-11 (with 11 being the latest major release of GCC), you instead get the following:

GCC successfully ran

This means GNU Compiler Collection was in fact installed successfully! So, you can compile and run a C++ program as follows:

Running a C++ program with G++

However, we would prefer not to specify the version number every time, so we can instead symlink GCC and G++ so that the computer does not think we are using Clang. Go back to the terminal type cd /usr/local/bin. From here run an ls and identify the latest version of GCC and G++ that you would like to symlink against. For me, that is currently gcc-11 and g++-11.

Next, run ln -s gcc-11 gcc and ln -s g++-11 g++. Finally, log out and back into your Mac. (If you do not, you will again see the Clang error message when running GCC!) Now, when we run GCC we get the following:

GCC alias successfully created

Now we can simply type the following to compile our C++ programs:

Running a C++ program with the new G++ alias

Common Pitfalls

When first setting up GCC, I ran into several issues due to my lack of familiarity with the macOS ecosystem. Firstly, if you are running into Homebrew issues, I recommend running brew doctor. This will give you a list of warnings from Homebrew that might help you notice any irregularity.

Next, I also had an issue that was fixed by simply uninstalling and reinstalling the CLT (Command Line Tools) after I had installed GCC via Homebrew. This can be done by running rm -rf /Library/Developer/CommandLineTools and then xcode-select --install. Logging out and then back in between steps may also prove useful. Beyond this, please leave a comment below if you have any other issues or questions.

Reasons for Switching

While I have some nostalgic affection for GCC, I was driven to switch for two main reasons. The first is GCC allows the use of the bits/stdc++.h header include file. Secondly, it has some unique and helpful compile flags. Many competitive programmers find it convenient to paste the following include at the top of their C++ programs: #include <bits/stdc++.h>. This includes header bundles many of the most common libraries into a single include statement and removes the need to include each individually (i.e., vectors, hash maps, etc). However, using #include <bits/stdc++.h> in any other context should be discouraged, as it adds unnecessary overhead to the program.

Secondly, there are some flags that have no effect when using Clang. This list of diagnostic flags in Clang shows more than 30 flags that exist for GCC compatibility but have no effect in Clang. Another benefit for competitive programming (CP) is using GCC pragmas. For example, many competitive programmers throw the following line at the top of their C++ programs: #pragma GCC optimize("O3,unroll-loops"). This code turns on the O3 and unroll-loops optimization flags for GCC. Both of these may speed up program execution in some cases.

The Case for GNU Compiler Collection

In conclusion, I think long-time Linux users, as well those interested in CP, will benefit greatly from using GCC instead of Clang on their computers running macOS. Furthermore, I think it’s at least worth trying GCC to see how it works for you, as Clang will still be installed on your computer and you can switch back and forth at any time. I will still mostly be using my dual-boot Ubuntu setup for CP and other general C/C++ programming, but I appreciate having a consistent setup on my Mac when the need arises.

Conversation
  • Nnaemeka says:

    Thank you so much for this!

  • Comments are closed.