26

I have seen recently that #include <bits/stdc++.h> includes every standard library and stl include file. When i try to compile the following code segment with visual studio 2013, it gives an error C1083: Cannot open include file: 'bits/stdc++.h': No such file or directory. But it works perfectly fine with codeblocks. Is there any way to avoid this error?

#include <bits/stdc++.h>

using namespace std;

int main()
{

}   

I saw in some post that 'the header file is not part of the C++ standard, is therefore non-portable', and should be avoided'. But i think it is helpful in contest programming.

  • 9
    That isn't a standard header. Don't rely on implementation details. If you really need a header that includes all the others (you really don't want that), use your own. – chris Aug 16 '14 at 05:46
  • I would've used my own but it won't be recognized in the popular OJ sites like uva,loj etc. Plus why it does not work in VS2013? Just wanting to know that as it works in codeblocks. –  Aug 16 '14 at 06:08
  • 2
    Because they're using different standard library implementations. – chris Aug 16 '14 at 06:08
  • 16
    It doesn't work in VS2013 because only GCC (used by Code::Blocks) has that file. This is like asking why you can't `#include ` on a Linux system. – Wyzard Aug 16 '14 at 06:12
  • 1
    Voting to reopen. This is most definitely not a duplicate of [Why should I not #include ?](https://stackoverflow.com/q/31816095/4561887), and it's definitely a stretch to say it is. This question seeks explanations of why it doesn't work and would be benefitted by solutions of how to make it work anyway. The other question seeks neither of those two things. A link to that other question should be posted in the comments here as a _related_ question, not a duplicate. – Gabriel Staples Apr 23 '22 at 12:13
  • @GabrielStaples: I would say the other question is related to such extent that it becomes nearly a duplicate. The most voted answer literally says: for avoid errors with the header drop its usage. And the duplicate question explains: the header is gcc-specific; this is exactly the reason why it doesn't work in Visual Studio. "... and would be benefitted by solutions of how to make it work anyway" - There are solutions "copy the header from gcc" and "create the header by yourself". Do you want to post another solution? If not, then I assume the duplicate to be the best state. – Tsyvarev Apr 24 '22 at 20:02
  • @Tsyvarev, I don't personally want to add another answer at this time, but I do think the "copy the gcc header" solution is a great solution, and could use enough improvement that I'd like to open the question to allow someone to provide an improved solution to better demonstrate and explain that. – Gabriel Staples Apr 24 '22 at 21:21
  • In C/C++ Configurations inside vs code, make sure you have set Compiler path because by default it is not set. Also, you may set IntelliSense mode to windows-clang-arm64 – Akash papnai Apr 30 '23 at 05:59

6 Answers6

50

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.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
17

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 same include folder mentioned above.
  • Make a new directory with name bits and paste the stdc++.h file inside the bits folder.
  • Restart Visual Studio if it is running and use it
Raza
  • 343
  • 2
  • 4
  • 10
    Please avoid using external links to websites like mega. They will likely be broken in a near future. – Jérôme Richard Mar 08 '21 at 19:02
  • 1
    Here is the reliable way to find stdc++.h. Search for "gnu.org stdc++.h" by your favorite search engine. It should appear in the first hit or the first few hits. – burnabyRails Mar 09 '21 at 00:34
  • 3
    Here is a link to the file of interest in the GCC source code directly: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h As predicted by @JérômeRichard, the link in the answer is already dead. – Gabriel Staples Apr 23 '22 at 12:10
5

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.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41
-1

My answer will be for Enterprise 2015, Version 4.8.03761, update 3.

When you installed codeblocks, MinGW (the (Min)minimalist (G)GNU Compiler Collection (GCC) for (W)Windows) was also installed. Or you could install it separately.

I believe this problem is because Visual Studio can't find bits/stdc++.h in its Include Directory (Project -> YourProject Properties -> Configuration Properties -> VC++ Directories -> Include Directories -> press the drop down arrow -> <Edit..> -> Evaluated Value).

You need to go to that directory, and paste the entire bits folder from your MinGW installation, mine was in (C:\MinGW\lib\gcc\mingw32\8.2.0\include\c++\mingw32).

No errors on my side once that was done. I hope this answer will help anybody looking to solve this problem.

Thank you.

dallasarch
  • 43
  • 4
-1

To use bits/stdc++.h you can create bits folder in below directory in your computer and paste the stdc++.h file in bits folder and your good to go.

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include
SaAvN
  • 23
  • 1
-3

Install latest version of MinGW from the official release

Then copy the bits folder from

C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++\mingw32\bits

to...(VS2019)

C:\Program Files (x86)\Microsoft Visual Studio\2019\<type>\VC\Tools\MSVC\14.27.29110\include

Note the would be "Community"/"Professional" based on the VS installation and licensing.

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
getsuha
  • 711
  • 6
  • 16