11
#include <bits/stdc++.h>

If I put the above line at the top of my program.cpp file, it gives me the following error message:

cannot open source file "bits/stdc++.h"

How can I fix this?

Gooz
  • 1,106
  • 2
  • 9
  • 20

2 Answers2

10

It's an internal GCC header file. There is no guarantee that it will work anywhere else; even using it with GCC itself is poor practice for many reasons. Do not use it, ever.

How can I fix this?

Include those standard headers which you actually need. For example, if you need std::cout, then include <iostream>. If you need std::string, then include <string>. If you need std::ifstream, then include <fstream>.

As those are standard headers, they are guaranteed to work everywhere.

cppreference.com is a good free online source to find out which headers are needed for which component of the standard library. Let's take a non-obvious one, like std::ifstream. You just search for that name and you'll find http://en.cppreference.com/w/cpp/io/basic_ifstream. There, it says:

Defined in header <fstream>

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
6

That is not a Standard C++ header file, and Visual C++ does not implement it. You should not use it even if the compiler you are using does implement it, as it makes your code immediately non-portable, possibly even between different versions of the same compiler.

  • 13
    The question is how to fix not why not to fix. Sure this is not good practice but this is a very commonly used in competitive programming and good for quick prototyping. – Andrew Apr 16 '22 at 16:20