#include <bits/stdc++.h> with visual studio does not compile
This is non-standard header file, which is known to be part of the libstdc++, which typically comes with GCC. However VS comes with Microsoft's own implementation of the STL, which doen't contain such file. I'd recommend to stop using it and instead include necessary standard headers, like <iostream>
, <vector>
, <string>
, <algorithm>
etc.
To use bits/stdc++.h
preprocessor in Visual Studio you need to download the stdc++.h
file from the given below link.
- Download file from the following link: https://mega.nz/file/feIXkY7a#ECkj1bZKA-72dYzatuKsgBQJ4lMe0v81sfxuhyKCNk0
- Create bits folder in the following directory:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include
- In some cases, the folder
C:\Program Files (x86)\Microsoft Visual Studio\2019\
might be empty. In such cases, go to vs code and type#include <
at the top. You'll get auto complete suggestions . Along with each suggestion, a path should be visible for every suggestion. Copy the path and paste it on the file explorer. The path will land you inside the sameinclude
folder mentioned above. - Make a new directory with name
bits
and paste the stdc++.h file inside thebits
folder. - Restart Visual Studio if it is running and use it
Is there any way to avoid this error?
Yes: don't use non-standard header files that are only provided by GCC and not Microsoft's compiler.
There are a number of headers that the C++ standard requires every compiler to provide, such as <iostream>
and <string>
. But a particular compiler's implementation of those headers may rely on other non-standard headers that are also shipped with that compiler, and <bits/stdc++.h>
is one of those.
Think of the standard headers (e.g. <iostream>
) as a "public" interface, and compiler-specific stuff (like everything in bits/
) as the "private" implementation. You shouldn't rely on compiler-specific implementation details if you want your program to be portable to other compilers — or even future versions of the same compiler.
If you want a header that includes all the standard headers, it's easy enough to write your own.