macOS ships by default with the LLVM/Clang compiler which does not
support GNU/GCC extensions such as the bits/stdc++.h
header file that
every beginner tends to use in their C++ code.
This is a problem even when you use g++ in macOS, as Apple use g++ as a front-end to clang++ for legacy reasons.
-
You need to create a directory ‘bits’ under the folder /usr/local/include. This can be done by running the following command in the Terminal:
mkdir /usr/local/include/bits
-
You need to copy the contents of the stdc++.h file included in this repository into the newly created bits folder:
curl https://raw.githubusercontent.com/khaveesh/macOS-stdc.h/master/stdc%2B%2B.h > /usr/local/include/bits/stdc++.h
-
Profit! You can now write:
#include <bits/stdc++.h>
There is a reason why Apple (Clang) and even Microsoft (Visual C++) do not include this header file in their compilers. It’s really bad coding practice to include this file.
bits/stdc++.h is a non-standard header file containing a list of all header files available in the GCC stdc++ library. Everyone who has written at least a moderately complex program in C++, knows how much of a pain-in-the-a** it is, to find the appropriate header file for all the builtins used. So it is an easier way out to just include bits/stdc++.h. But this means that all header files regardless of whether you’re using them or not, are included in your code. This increases the compilation time of your code. This is especially detrimental in Competitive Programming where coders usually change one or two lines at max and frequently recompile when debugging. Thus this delay in compilation adds up. So it is better to spend some time to analyse which functions you’re using and include only the minimal number of required header files.
Copy and paste the following commands in your Mac terminal:
-
mkdir /usr/local/include/bits
-
https://raw.githubusercontent.com/khaveesh/macOS-stdc.h/master/stdc%2B%2B.h > /usr/local/include/bits/stdc++.h
But this is bad coding practice and you should consider this only as a stopgap solution and refrain from using it.
The stdc++.h file is derived from the latest stdc++.h file in the GCC git repository with the non-Mac include directives removed.
Fun Fact: This license was one of the primary reasons why Apple decided to fund the fledgling LLVM Compiler team in the University of Illinois. Read Apple’s Great GPL Purge to know more.