2

I have a program which includes lots of header files but it do not uses all the header files. I have removed some of them although it is working fine. I did not notice any changes in the performance. Will this affect code size or something else. Can I include header files as much as I want without affecting the code size and performance.
I mean if I include all headers in a separate header file then I call only this header file in my all programs. Will it work normally?

  • Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. – gnat Feb 4 '16 at 13:11
  • 2
    @gnat: Seems pretty clear to me. If you don't know the answer then you don't have to write one. – Lightness Races with Monica Feb 4 '16 at 13:28
  • @LightnessRacesinOrbit if you know about header files in Visual Basic or Java, you have my deepest respect and blessing to answer any question about Life, Universe and Everything – gnat Feb 4 '16 at 13:54
  • @gnat: What are you talking about? There are no header files in either Visual Basic or Java, and the question makes no reference to either language. – Lightness Races with Monica Feb 4 '16 at 14:03
  • 1
    @gnat: What's the point? As you've already pointed out, other languages don't have headers. So how could the OP possibly be asking about them? This question is completely clear; please stop searching so hard for reasons to close and downvote questions. – Lightness Races with Monica Feb 4 '16 at 15:10
2

Generally speaking, including extra header files shouldn't increase the size or impact the performance of your compiled code, but it's still a very bad practice.

When you're working on a project that has hundreds or thousands of source files, you want to rebuild as few files as possible when making a change to a header file. If every source file includes every header file, then you'll need to rebuild everything any time you touch any header file.

On a related note, whenever possible use a forward declare instead of including a header file in another header file, for example, do this:

class bar;
class foo
{
  private: bar* that;
}

instead of:

#include <bar.h>
class foo
{
  private: bar* that;
}
1

If you're talking of C or C++, then no - all that happens is that your compile time is slowed as the compiler has to read and parse all the extra header files.

This can be significant, especially if headers include other headers that include.. you get the idea. Compilation time can increase dramatically (so much so that some compilers like visual studios cache the results in 'pre-compiled headers')

The compiler produces binary executables that do what the code tell it to do, so any un-needed header information is simply ignored. The same can be said for some code, if the compiler can tell it is not needed (eg inline functions) then that code may not be turned into part of the output binary.

Too many headers can lead to difficult to trace compiler bugs, if you define something in two headers and include both, you can end up using the wrong definition. As a result, it is always best to try to use as few headers as are necessary.

  • Well, technically including a header could completely ruin the performance of your program whether you go out of your way to name one its symbols or not. /* sillyheader.hpp */ struct Silly { Silly() { std::thread([]{ while (true) {} }); } }; static Silly s; Just sayin' ;) – Lightness Races with Monica Feb 4 '16 at 13:25
  • @LightnessRacesinOrbit sure including a header that contains a static means adding program code, but I hope I answered the spirit of his question. – gbjbaanb Feb 4 '16 at 13:29
  • Might be worth pointing out as a caveat that headers can autonomously "execute" things, but it's not a condition for my upvote :) – Lightness Races with Monica Feb 4 '16 at 13:30
  • @LightnessRacesinOrbit Your comment says it well. Maybe a new question needs to be raised: "If a colleague puts a static instance of a class in a publicly visible header file, should we remove his C++ credentials and put him in the VB team?" :-) – gbjbaanb Feb 4 '16 at 13:32
  • Well this is my point - something like what my comment says could benefit the answer. But it would have to be in the answer, as comments are not part of answers but transient, temporary critiques and whatnot. Just a thought; you don't have to use it. Have a nice day – Lightness Races with Monica Feb 4 '16 at 13:41

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.