Using <bits/extc++.h> Header File on MacOS (Example with VSCode)
Introduction
To conclude first, it’s difficult to use the <bits/extc++.h>
header file with Clang as the compiler, as it will generate various mysterious errors. This article adopts the method of installing GCC via Homebrew and using GCC as the compiler.
I originally wanted to try pbds while doing CSES problem set, but I found it completely unusable. There seems to be no solution on the internet, so after some research, this article was born.
Install Homebrew (if not already installed)
1 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
Install GCC
Here you can choose to install different versions of GCC, but note that Macs with Arm architecture (M series chips) only support GCC version 11 and above.
1 | # Install the latest version of GCC |
Use GCC Compiler
If you don’t know the installed GCC version, you can use the following command to check:
1 | ls /usr/local/bin/gcc* # Intel Mac |
Once you’ve confirmed the exact version number of GCC (for example, gcc-11), you can use that version of GCC to compile your code. For example, if the GCC version is 11, run:
1 | g++-11 example.cpp -o Example |
Use GCC in VSCode
Use C/C++ Compile Run Extension
Go to the extension settings page, search for C-cpp-compile-run: Cpp-compiler
, and set it to use gcc for compilation.
Use Competitive Programming Helper (cph) Extension
Go to the extension settings page, search for cph.language.cpp.Command
, and set it to use gcc for compilation.
Conclusion
Now you can use the <bits/extc++.h>
header file on MacOS. You can try whether the following code runs correctly:
1 |
|
If you have any questions about using GCC and the <bits/extc++.h>
header file on MacOS, feel free to discuss in the comments below.