14

Is there any expression possible for the syntax to include multiple headers at once, with no need to write the "#include"-expression for each file new?

Like, for example:

#include <stdio.h>, <stdlib.h>, <curses.h>, <string.h>  /* Dummy-Expression 1. */

OR

#include <stdio.h> <stdlib.h> <curses.h> <string.h>     /* Dummy-Expression 2. */

Question is for C AND C++.

  • 1
    One big problem with the home grown header file that contains lots of `#include` statements is that when one of those many header files changes, everything needs to be re-compiled/re-linked/re-installed. This is a major PITA in a production environment – user3629249 Oct 12 '19 at 13:41

6 Answers6

11

No, there is no way to do this. You have to type out (or copy) each #include to its own line, like this:

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <string.h>

This applies to both C and C++.

Some of the other answers discuss creating another header file that includes each of these, but I'm not going to discuss doing that. It in general is a bad idea and causes issues like namespace pollution and the need to recompile when you change that header file.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
9

No, there is not.

Write an #include directive for each inclusion operation you wish to perform.

You could, however, have a "utility" header that does nothing but include many other headers that you use frequently. Then you just include that one utility header. Whether this is a good idea or not, is a matter of opinion.

If you go down that route, don't be tempted to start relying on internal implementation headers.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
5

It can be done with a help of auxillary header including header:

#define HEADERS (<stdio.h>)(<stdlib.h>)(<curses.h>)(<string.h>)
#include "Headers.inl"

Where Headers.inl performs include for each item in HEADERS sequence:

// Headers.inl
#include <boost/preprocessor/seq.hpp>

#if(0 < BOOST_PP_SEQ_SIZE(HEADERS))
#include BOOST_PP_SEQ_ELEM(0, HEADERS)
#endif

#if(1 < BOOST_PP_SEQ_SIZE(HEADERS))
#include BOOST_PP_SEQ_ELEM(1, HEADERS)
#endif

#if(2 < BOOST_PP_SEQ_SIZE(HEADERS))
#include BOOST_PP_SEQ_ELEM(2, HEADERS)
#endif

#if(3 < BOOST_PP_SEQ_SIZE(HEADERS))
#include BOOST_PP_SEQ_ELEM(3, HEADERS)
#endif

…

It can probably be simplified by letting boost preprocessor handle the all the repetition with BOOST_PP_SEQ_POP_FRONT and recursive include of itself.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • 2
    This is just too much preprocessor trickery for an answer that should be as simple as "No". Still pretty interesting, though. – S.S. Anne Oct 12 '19 at 14:22
4

You can make a header file with all the common includes and include that in your files:

File common.h:

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <string.h>

and in your file you can include that file instead:

#include <common.h>
sephiroth
  • 896
  • 12
  • 22
  • 1
    I mentioned the same in my own answer. It, of course, works - but it's usually a terrible thing to do. It ends up including unnecessary headers, which pollutes the global namespace (if done globally) and gives the compiler extra work to do. Your answer (IMHO) should mention that and *discourage* this solution. – Jesper Juhl Oct 12 '19 at 13:41
  • @JesperJuhl "it's usually a terrible thing to do." is too broad a statement. Spending time to include just the needed standard headers (as per OP examples) is not efficient use of programmer's time for *.c files. Code to your group's standards. Sure you might save modest compile time. Further, if a collision occurs because ``, etc. was included even though standard string functions were not used in the .c file. I'd like to know that. – chux - Reinstate Monica Oct 12 '19 at 19:28
  • 1
    @chux Don't underestimate how much unnecessary includes can slow down a build - and hence programmer time. The codebase I currently work on used to take >25min to build on a 20 core (40 threads) machine. After spending some time sanitising our headers (and a few other minor things), we now build that code (from scratch) in ~6minutes. That's *significant*. Sure, a single include doesn't matter. But when it's multiple includes in thousands of files, it adds up and it increases dependencies between modules that would not have had to be rebuilt without those includes. – Jesper Juhl Oct 17 '19 at 13:54
3

There's no syntax for that. But if you really want something like that, then just create a new header that contains nothing but multiple #include statements for the headers you want and then #include "combo-header.h" whenever needed. I'd discourage this though. Better to only include what you use explicitly where you use it - for several reasons; compile time and namespace pollution being the top ones.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
-1

Yes, you can use #include<bits/stdc++.h> This header file includes all the standard header files. The only problem is, it would include a lot of unnecessary files which you don't require for a program, and also, since it includes a lot of header files your compilation time will increase. If there are some particular headers you use quite often in a program then you can make a header file of your own and include the required files in the one you created.

Sahil Singh
  • 79
  • 10
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 24 '21 at 18:33