Problems of including too many header files in C

Viewed 692

Does including too many header files increase the size of the source file. Does it also increase the size of executable? Do these header files increase the compilation time?

For example if i add these header files in my program do they increase of size of source file or executable file or both?

#include <stdio.h>
#include "header1.h"
#include "header2.h"

What are the other problems of including too many header files?

5 Answers

Does including too many header files increase the size of the source file.

It increases with as many letters as you type. So if 1 letter is 1 byte on your system, then adding #include <stdio.h> increases the source code file size by at least 18 bytes. This shouldn't matter to you unless you are using a computer from the mid-1980s.

Does it also increase the size of executable?

No. Only used functions increase the size of the executable.

Do these header files increase the compilation time?

Generally yes, though compilers use various tricks such as "precompiled headers" for its own libraries. Again, this isn't a problem unless you are using 1980s stuff or worse (such as Eclipse).

What are the other problems of including too many header files?

Your main concern about including headers should be to not include stuff that you don't use. Every include creates a dependency, and also means more identifiers and symbols added to the global namespace.

Header files should not produce any extra code unless they are poorly designed, an d you will probably encounter redefinition problems if they do and you include them more than once. By code here I mean "machine code", that is executable code.
About the source code, the compiler ultimately sees the source code as one big source code with the #include directives replaced by the content of the file, so adding more header files will increase the compile time (as the apparent source code will be longer). So including unnesessary files should be avoided.

Does including too many header files increase the size of the source file.

Yes, For each additional character added to a source file, for example "#include <stdio.h>" increases the physical size of the source file precisely by the number of characters in that statement, eg": strlen("#include <stdio.h>"); bytes. (and, depending on how OS allocates file block size, it could be seen by the OS as an extra kByte.) More importantly though, at compile time the contents of each header file #included will effectively be expanded into source code that is fed to the compiler.

Does it also increase the size of executable?

Yes/No/Possibly. Depending on what is actually used in the header file. Optimizing compilers can exclude whatever is not needed in an executable. If nothing is used, there will no additional size to the executable. There will however be additional work done during compile-time because even if there is nothing useful in the header file, compiler does not know this until it is processed.

Do these header files increase the compilation time?

Compared to what? i.e. If there are, within a header file, necessary components to allow a build to occur, i.e. by containing prototypes of functions, #defines, etc, then compile time is just normal compile time. But if you have been compiling with say 3 necessary header files for awhile, and decide that you want to add a new library (and it's corresponding header file.), then by all means, yes, the next compile will take a little longer than those previous.

What are the other problems of including too many header files?

Too many?. If each and every header file is necessary, then there are not too many. (with this caveat about maximum header file depth.) However, if the question asked about unnecessary, the short answer would be among other things code bloat. It will add to compile time because each header file, regardless of whether there is anything useful in it has to be looked at by the build process. But, IMO, even worse unnecessary header files will add to the difficulty of maintaining the code. The unfortunate person that comes along 3 years later to update the code may have difficulty in understanding what is what with all the header files?

There is a good post here discussing this in more detail.

Additionally, this is a fun page that also discusses header files.

Adding header files will increase the size of the intermediate source file, taking into account the inclusions. Modern compilers may not even generate this intermediate file explicitly -- it may be absorbed into the overall compilation process. This is a matter of compiler design. As a developer, you probably won't ever see the fully-expanded file unless you ask for it (e.g., gcc -E).

Adding header files will not necessarily increase the size of the compiled code -- if all the headers contain is declarations and constant definitions, they won't increase the size much, if at all. If they contain actual code -- which isn't a particularly common practice -- they might have some small effect on the executable size.

Adding header files will probably have some effect on the compilation time but, really, this isn't a question anybody should be asking. If you need the headers, you need the headers. If it slows down compilation, what's the alternative? Don't compile?

If the question is really about how to distribute code between headers and source files, so as to improve some aspect of the build process, then that's a very complicated question to answer. If the question is about what harm is done by including a bunch of headers you don't use, the answer with modern compilers is: very little, from a functional perspective. However, including some header gives the reader the impression that the source actually uses the features it declares, and that's bad for readability. You should do your future self, or your colleagues, a favour and try not to include headers that aren't used. But if you need them, you need them, and there's little point worry about the consequences too much,

Does including too many header files increase the size of the source file?

The more characters in the source file, the more size has the source file.

But it's only about the #include directives itself. Not the content of header files - the source file doesn't get expanded by the content of the headers.

When you #include a header, the compiler gets known about to read it at that point of time, but the source file isn't changed.

So, yes.

Does it also increase the size of executable?

Depends on the content of the headers. If they contain definitions then yes.

Do these header files increase the compilation time?

The more to read and evaluate from the compiler, the longer the time to compile. So yes.

What are the other problems of including too many header files?

As said before, the time to evaluate might take longer and thus the more header files, the slower the compilation. But there is nothing wrong to add as much useful headers as you like. Just don't add unnecessary headers, which slow down the compilation.

Related