1

In java we can import all class from a package using '*' like - java.lang.*.

While coding in C++ we imports multiple library like this -

#include<cstdio>
#include<iostream>
.....

Is there any shortcut/way in C++ to include all these library using a single statement/line?
Thanks

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
Razib
  • 10,057
  • 10
  • 46
  • 71
  • 1
    Make your own header that includes what you need. But usually, you would include the minimum of what you need in each file. It's useless to include headers you wont use in a file. – Rosme Jan 15 '15 at 20:54
  • 1
    Without precompiled headers it adds to compile time to include stuff you are not using. That may not be a problem when your program is 100s lines and 1/2 dozen source files but when your program takes 30 minutes to build because it has 1000s of source files and each including 100s of headers you may spend a long time reducing the waste. – drescherjm Jan 15 '15 at 21:09

4 Answers4

3

No, there is no method to specify more than one file in a #include preprocessor directive.

Many people get around this dilemma by creating a monster include file that has multiple #include statements:
monster_include.h

#ifndef MONSTER_H
#define MONSTER_H
  #include <iostream>
  #include <string>
#endif

The disadvantage is that if any of these include files are changed, including ones not used by the source file, the source file will still be rebuilt.

I recommend creating an empty stencil header file and an empty stencil source file, then adding #include as required. The stencil can be copied then filled in as appropriate. This will save more typing time than use the megalithic include file.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
2

There's nothing available for c++ like in your java sample.

Roll your own header to include all stuff you need.

E.g.

AllProjectHeaders.h


#ifndef ALLPROJECT_HEADERS
#define ALLPROJECT_HEADERS

#include<cstdio>
#include<iostream>
// ...

#endif
πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
2

You can use this library:

#include<bits/stdc++.h>

This library includes every library you need. Using this, you can delete (or comment) all the others library declarations.

See more here: How does #include bits/stdc++.h work in C++?

Community
  • 1
  • 1
Thiago Souza
  • 865
  • 3
  • 12
  • 28
  • You [should not include stdc++.h](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – rsjaffe Jan 12 '19 at 22:22
1

You might also want to take a look at precompiled headers, it should reduce the number of includes in the source files if there is something that you include everywhere.

bialpio
  • 954
  • 6
  • 17