Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cannot open source file bits/stdc++.h" in Visual Studio [duplicate]

#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?

like image 492
Gooz Avatar asked Mar 31 '26 00:03

Gooz


2 Answers

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>

like image 147
Christian Hackl Avatar answered Apr 02 '26 03:04

Christian Hackl


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.