How Does #Include ≪Bits/Stdc++.H≫ Work in C++

How does #include <bits/stdc++.h> work in C++?

It is basically a header file that also includes every standard library and STL include file. The only purpose I can see for it would be for testing and education.

Se e.g. GCC 4.8.0 /bits/stdc++.h source.

Using it would include a lot of unnecessary stuff and increases compilation time.

Edit: As Neil says, it's an implementation for precompiled headers. If you set it up for precompilation correctly it could, in fact, speed up compilation time depending on your project. (https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html)

I would, however, suggest that you take time to learn about each of the sl/stl headers and include them separately instead, and not use "super headers" except for precompilation purposes.

Why should I not #include <bits/stdc++.h>?

Including <bits/stdc++.h> appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year.

I imagine the advantages are vaguely given thus:

  • You only need write one #include line.
  • You do not need to look up which standard header everything is in.

Unfortunately, this is a lazy hack, naming a GCC internal header directly instead of individual standard headers like <string>, <iostream> and <vector>. It ruins portability and fosters terrible habits.

The disadvantages include:

  • It will probably only work on that compiler.
  • You have no idea what it'll do when you use it, because its contents are not set by a standard.
  • Even just upgrading your compiler to its own next version may break your program.
  • Every single standard header must be parsed and compiled along with your source code, which is slow and results in a bulky executable under certain compilation settings.

Don't do it!


More information:

  • #include <bits/stdc++.h> with visual studio does not compile
  • How does #include <bits/stdc++.h> work in C++?

Example of why Quora is bad:

  • Is it good practice to use #include <bits/stdc++.h> in programming contests instead of listing a lot of includes?

Why is there a .h extension in <bits/stdc++.h>?

C++ does not use the .h extension for standard library headers named in #include statements.

But bits/stdc++.h is not a standard header, so the standard library naming conventions wouldn't apply to it. It should never be used anyway.

There is no mandatory mapping, IIRC, from the name used in the include statement, to the filename. You can certainly use .h extensions on your own headers if you want, although it may get confusing if you mix C and C++ in a project.

How can I include <bits/stdc++> in Xcode

You can do it by copying stdc++.h file from here:
https://gist.github.com/reza-ryte-club/97c39f35dab0c45a5d924dd9e50c445f

Then you can include the file in your c++ file like this:

 //suppose the file is in your home folder, here my username is reza
#include "/Users/reza/stdc++.h"

Using <bits/stdc++.h> gives a global variable ambiguity

std::count is a function from the standard library.

http://www.cplusplus.com/reference/algorithm/count/

Since you use the namespace std , "count" can refer to either std::count or the variable count.

You need to either rename your variable, or stop using the std namespace.

You can also include only the c++ headers that you need instead of bits/stdc++.h which includes all of them.

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

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.

How to Precompile <bits/stdc++.h> header file in ubuntu 20.04?

So, After having help from @TedLyngmo's answer and doing a little bit more research, I decided to answer the question myself with more clear steps.

PS: This answer will be more relatable to those who are using sublime with their custom build file and are on Linux OS (Ubuntu).

  1. You need to find where stdc++.h header file is present, so open the terminal and use the command:

    find /usr/include -name 'stdc++.h'

    Output:

    /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h

  2. Go to the above location and open the terminal there and now we are ready to precompile bits/stdc++.h header file.

  3. Use the following command:

    sudo g++ -std=c++17 stdc++.h

  4. You'll observe stdc++.h.gch file is now created implying that precompiling is done.

PS: You need to use sudo as we need root privileges when g++ makes stdc++.h.gch file.

NOTE: Here as I was using the c++17 version in my custom build file so I mentioned c++17, you can use whatever version you are using.

It worked perfectly for me so I hope it helps you too!

#include <bits/stdc++.h> with visual studio does not compile

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.



Related Topics



Leave a reply



Submit