Search code examples
c++compiler-errorsmingwheader-filespreprocessor-directive

Why #include<bits/stdc++.h> does not work by default on windows?


I have installed the mingw-w64 compiler on windows. But using #include<bits/stdc++.h> in the c++ program preprocessor directive always gives an error. How can this be fixed?


Solution

  • bits/stdc++.h is not a standard header file. Thus, it is not guaranteed to work except with certain specific compilers. Even different versions of the same compiler might or might not provide it.

    When it is available, bits/stdc++.h just #includes every standard C++ header file. This might make sense for someone just starting out in the language, so they don't have to worry about figuring out which includes they need and which they don't. But using it slows down compile time, might in certain cases make the executable bigger than it needs to be, and (as you discovered) makes the code non-portable.

    The only solution to this is to not use it, and instead, #include just the specific headers you need. You're really "supposed" to do it as you program; when you need a certain function declared in a header, you include that header, then write the function call. Some IDEs will tell you which includes you need for each function.

    But if you've already gotten the code all written, you can cheat. Just delete the #include <bits/stdc++.h> line and try to compile. If the compiler complains about missing or undefined symbols, google the symbol to figure out which header it comes from, and #include it. Rinse and repeat until you get a clean compile.