I always wondered what are the effects of including not needed headers to the final executable once compiled. In my code some times I may included many different headers that I do not need (or I used to need but now I don't and forgot to remove them). Now before compiling the final executable I always clean up my header files. But I am wondering, is there a problem if I don't?

I am aware that in C# having unused Using in my code only reduces (slightly) the speed of intellisense and the compiler only includes stuff that is really needed. Is the same thing true for C and C++? What about Java and other languages?


Importing in Java, C# and the like is fundamentally different from including in C and C++:

The former will add the named modules to those considered for symbol resolution, the latter inserts the named headers content literally into the translation unit.
Which means a header can contain anything the source-file can, including translation-unit-local objects (They might have a constructor and destructor in C++), functions, structs (and for C++ classes).

Which is not quite as bad as it sounds for C, as headers tend to mostly contain forward-declarations, struct definitions and preprocessor-macros.

In C++ on the other hand, templates (which are far more powerful than puny Java or C# generics) generally must be implemented in headers, which makes them grow to considerable size and complexity (often outstripping by orders of magnitude the source-file of the translation-unit).

The C++ committee is working on a module-system to take care of that, but at the earliest it will be there in C++17 (and don't hold your breath yet).

So, for the languages with module-system (Java, C#) you will only have a slight slowdown for analysis and compilation.
For C, you will probably only have a slightly higher slowdown, due to literal inclusion of the source, though at least they tend not to be too big and complex.
For C++, you will have a moderate to severe slowdown of compilation and analysis, due to heavy use of templates and inline-functions.

In both C and C++ headers can in theory contain TU-local objects, which would result in some runtime overhead.
While I know that such is used by C++ for <iostream> (look for ios_base::Init in the standard) to initialize the C++ streams early enough, I don't know of any use in C.


If you are not using precompiled headers then each include means that the header is included fully into each compilation unit. That means reading the file and putting it through the same pipeline of the rest of the code and filling the symbol table.

So yeah unused includes will slow down compilation.

Some compilers will have optimizations in place that alleviates most of the parsing work by creating precompiled headers that has just the symbols and templates in a proprietary easy to parse format. However doing the lookup each #include will still take some time.

Java's imports are a bit different, when you import a package (for example import java.util.*) then the compiler will look through the directory/jar specified in the class path and make a map of the public classes in it so ArrayList gets translated to java.util.ArrayList. Similarly for importing a class; it gets looked up and added to the translation table. Doing a static import requires looking in the class file and parsing the public declarations in it.


One more downside in C/++ to including an unneeded header in a source file: if precompiled headers are not used, then changing that unneeded header file forces recompilation of that source file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.