Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include multiple header-files at once with only one #include-expression?

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++.

like image 478
RobertS supports Monica Cellio Avatar asked Oct 12 '19 13:10

RobertS supports Monica Cellio


People also ask

Can you include multiple header files?

Yes, you can use #include<bits/stdc++. h> This header file includes all the standard header files.

How can you ensure a header file does not get included more than once?

By placing all of your code in between the #ifndef and #endif , you ensure that it is only read once during the compilation process.

What is multiple inclusion of header files?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time.

Can you have multiple header files in C++?

Overview of how Visual C++ manages resource files and header files provides an overview of how the Resource Set Includes command in Visual C++ lets you use multiple resource files and header files in the same project.


2 Answers

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.

like image 50
S.S. Anne Avatar answered Oct 23 '22 21:10

S.S. Anne


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.

like image 39
Lightness Races in Orbit Avatar answered Oct 23 '22 21:10

Lightness Races in Orbit