I have done most of my contests using a 2015 MacBook Pro, and my experience, for the most part, was not as enjoyable as when I did contests using my Windows PC. I saw that my Mac compiled using C++98, even though my gcc compiler was up to date, so I could not use stuff like range-based loop, auto, etc in my code. I got used to it, but recently I started looking for a fix again, and luckily I was able to do it.
Installing GCC
Mac's default C++ compiler Clang does not include many of the libraries provided by GCC, which is regularly used by competitive programmers, like #include <bits/stdc++.h>
and Policy Based Data Structures. So you're missing out by not using GCC.
First set up homebrew in your mac and install gcc using brew install gcc
. To find more details about this step check the links provided at the end.
Using GCC to the fullest
GCC already supports up to the latest C++ version but is defaulted to older ones for some reason. To remedy this you first need to know the version of gcc you are using.
Open Finder and click Go > Go to Folder on the top menu bar.
Type /usr/local/Cellar/gcc in the search box and press Return to open.
Then go to (whatever version number is here) -> bin. Here in the
bin
folder, you can see the gcc and g++ versions.
Now that you know the version of your g++ compiler, the next time you compile a c++ code instead of using just g++
use g++-13
instead (I used 13 since that is my g++ compiler version).
Now you can use everything from #include <bits/stdc++.h>
to Policy Based Data Structures
. However, for some reason this still compiles using c++17, which is fine, but if you want to use C++20 (which is the latest version as of writing this) just add -std=c++17
after g++-13
.
So from now on, instead of executing your code using g++ <filename>.cpp
, use g++-13 --std=c++17 <filename>.cpp
.
Configuring VS Code
If you use VS code instead of compiling through the terminal like me, you can easily implement this. If you are using code runner for execution do the following:
Go to
Code>Preferences>Settings
, then in the search bar at the top of settings typecode-runner.executormap
.Select
Edit in settings.json
, A JSON file will open.from thissettings.json file change the line with the key
cpp
. This is basically what VS code uses to compile your C++ code and show it to you. So replace theg++
here withg++-13
and add-std=c++17
after this.