I believe many MacOS users have encountered this problem (and it has been lasting for the entire history of programming) so I decided to post a blog to
gain free contributions help them install GNU's GCC. GCC has many useful libraries that Clang (Mac's default c++ compiler) doesn't have. Such as the classic
<bits/stdc++.h> and also the Policy Based Data Structures.
Today I saw a CSES problem that needed the indexed tree from pb_ds, so I started trying to get GCC, and it finally worked! After solving this problem, I decided to make this blog.
SO let's get to the tutorial. Have
Terminal.app opened up because we will be typing a lot of commands. Type
cd ~ in the Terminal and press enter, this makes the computer go to the root folder. I will assume that your Terminal is always in the root folder for this tutorial.
By the way, any time you think your Terminal is getting a bit too messy, type
clear to clean things up.
Setting up Homebrew
We will use Homebrew to install GCC, so we need to install Homebrew first. But first, make sure your Mac meets the Requirements. To install Homebrew, type (or copy/paste)
git clone https://github.com/Homebrew/brew homebrew into the Terminal and press enter. Now wait for the installation to finish.
Once Homebrew is installed on the computer, you can open up Finder and see that the "homebrew" folder is there. Now we need to tell our Mac where to access it, so in your Terminal type
pwd and press enter. This will print the parent path of the homebrew folder. Let's call this path
BREW_PATH for now.
To tell computer the path to Homebrew, take a look at the top of the Terminal. you should see a "zsh" or "bash". For example, this is the top of my Terminal:
If for you it says "zsh" type this into the Terminal:
open ~/.zshrc Otherwise if it says "bash" type this:
open ~/.bashrc (I'm not sure though because I have "zsh" but I think it's this) It should be an empty file, but if it's not empty, just keep whatever was there. Then add this to the file:
Remember BREW_PATH was the path we got from typing
Exit the file and go back to Terminal, then type
source ~/.zshrc or
source ~/.bashrc depending on which file you opened. This will run the file which includes homebrew's path. Now Homebrew has been set up and is ready to go!
Now that Homebrew is ready, it's time to download what you came here for. This process is actually much simpler than installing Homebrew. All you have to do is type:
brew install gcc --force-bottle
If it doesn't work, try:
homebrew install gcc --force-bottle
--force-bottle just makes sure homebrew downloads the precompiled version of gcc (which is a lot faster to install than the original version).
Viola! Now GCC is installed!
To compile codes with the newly installed GCC, we need to know the version of this gcc. Open up finder and go to the homebrew folder, then go to Cellar -> gcc -> (whatever version number is here) -> bin. Here in the
bin folder, you can see the gcc and g++ versions.
As you can see I got
gcc-11, so next time when you compile a c++ code, instead of typing
g++ A.cpp, type
g++-11 A.cpp. This will use the new GCC compiler instead of the old Clang.
Note: if you are using Sublime Text like me (or maybe some other editors or IDE that have this issue), you need to add
source ~/.zshrc or
source ~/.bashrc to your c++ build script to make it know where the path of the new compiler is.
If you want to only type
g++ A.cpp, you can open up the
~/.bashrc file again and add this:
alias g++='g++-11' alias gcc='gcc-11'
Don't forget to
And that's pretty much it. I hope I managed to help some people get GCC on Mac. :)