[Mac tutorial] Setting up C++ Compiler for Competitive Programming

Правка en8, от BunnyWarlock, 2023-12-21 19:44:22

I have done most of my CF 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 finally do it. Here are the steps, in case I have to do this again in the future.

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.

Make sure the terminal is at the root folder by typing cd ~. First set up homebrew in your mac and install gcc by typing brew install gcc in the terminal. 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.

  1. Open Finder and click Go > Go to Folder on the top menu bar.

  2. Type /usr/local/Cellar/gcc in the search box and press Return to open.

  3. Then go to (whatever version number is here) -> bin. Here in the bin folder, you can see the gcc and g++ versions.

Mine is version 13

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).

Congratulations you can now 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:

  1. Go to Code>Preferences>Settings, then in the search bar at the top of settings type code-runner.executormap.

  2. Select Edit in settings.json, A JSON file will open.

  3. From this settings.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 the g++ here with g++-13 and add -std=c++17 after this.

Here is my settings.json file

Personally, I use the Atom IDE, cuz I just got used to it over the years, and I got it to work there too by changing the setting of the Gpp compiler. For any similar type of IDE, I believe doing the same as me would also work.

Here is my setting configuration

References

Code I used to test my C++ compiler version. Don't ask me how it works, ChatGPT made this -_-

https://stackoverflow.com/questions/39091173/how-to-enable-c17-on-mac

https://codeforces.net/blog/entry/101012

Теги mac, gcc, c++20, compiler

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en11 Английский BunnyWarlock 2023-12-21 19:49:04 1 Tiny change: ' `g++-13 --std=c++20' -> ' `g++-13 -std=c++20'
en10 Английский BunnyWarlock 2023-12-21 19:48:31 4 Tiny change: ' --std=c++17 <filename' -> ' --std=c++20 <filename'
en9 Английский BunnyWarlock 2023-12-21 19:47:51 10
en8 Английский BunnyWarlock 2023-12-21 19:44:22 196 Tiny change: 'en.\n\n3. from this s' -> 'en.\n\n3. From this s' (published)
en7 Английский BunnyWarlock 2023-12-21 19:32:35 1269 Tiny change: 'oiler>\n\n<spoil' -> 'oiler>\n\n\n<spoil'
en6 Английский BunnyWarlock 2023-12-21 19:23:50 365 Tiny change: 'o do it.\n[cut]\n\' -> 'o do it.\n\n[cut]\n\'
en5 Английский BunnyWarlock 2023-12-21 18:37:23 1337
en4 Английский BunnyWarlock 2023-12-21 18:20:31 610 Tiny change: '/11080).\nFirst se' -> '/11080).\n\nFirst se'
en3 Английский BunnyWarlock 2023-12-21 18:03:31 364
en2 Английский BunnyWarlock 2023-12-21 17:55:57 33 Tiny change: '~~\n\n\n\n~~' -> '~~\n\n\n\n\n~~'
en1 Английский BunnyWarlock 2023-12-21 17:51:32 892 Initial revision (saved to drafts)