How To Include ‘bits/stdc++.h’ Header File With Clang Compiler on macOS
bits/stdc++.h is a non-standard C++ header file that is a part of the GNU C++ library. It includes ALL other header files. It makes your code concise, and saves you the hassle of including each header file that you use manually.
The catch is, this header file is only available with GNU’s GCC compiler out of the box. This is inconvenient for Mac users, as macOS ships with Clang compiler by default (which is better than GCC is many ways). You might think installing GCC compiler would solve the issue, however, GCC compiler on macOS does not ship with bits/stdc++.h
.
There have been solutions to this problem (like this one), but they’re not very elegant. In this article, I will show you how you can use bits/stdc++.h
with Clang compiler effortlessly and elegantly, so you can use it directly in any C++ project like this:
#include<stdcpp.h> // Refers to bits/stdc++.h
First off, you can download the header file here. This version is slightly tweaked as some header files from the original bits/stdc++.h file are not available to be used with Clang. Rest assured, it contains all the useful header files you’ll ever need to code in C++. I have named the file stdcpp.h
but you may choose to rename it however you wish. Note that the name you provide will be used exactly as it is when using the #include
directive.
To understand where exactly Clang compiler sources the standard header files from, we need to check the path that is associated with the -internal-isystem
option. You can get information about all options by running:
clang++ -v <file>
Use clang++ for compiling C++ files, and clang for C files. The -v
here stands for verbose output.
You should see something like this:
If you highlight all occurrences of -internal-isystem
, you should see this:
You can see the same paths mentioned at the bottom more clearly:
Clang looks for all standard header files in these paths. This makes our job easy. We simply need to take our stdcpp.
file and move it to one of the directories above. Let’s consider /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
.
If these paths are changed in the future, you’ll be able to find the new paths associated with
-internal-isystem
by following the same steps and use them accordingly.
With a finder window active, press ⇧ + ⌘ + G. Copy the path above and paste it, it will directly open the v1
directory. You should see this:
Now for the easy step, copy the stdcpp.h
file that you’d downloaded and paste it here. It will ask you to authenticate with a password as a normal user does not have permission to write to this directory.
Voilà, you’re set!
Try using #include <stdcpp.h>
(assuming you did not rename the downloaded file) in your C++ projects like so:
Notice the fact that I have used vector and unordered set, which would’ve required separate include statements without the stdcpp.h
header file. Upon compiling and executing, the file runs just fine:
TLDR
Download the linked file. Go to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
and copy the downloaded file to that directory. You can start using #include <stdcpp.h>
in your projects instead of manually including all required header files.
Thank you for reading my article. Consider dropping me a follow if you found this article helpful or learned something new. Till next time.