English 中文(简体)
Is it a good idea to put all project headers into one file HEADERS.h?
原标题:
  • 时间:2009-11-20 03:06:38
  •  标签:
  • c++
  • header

I talked to my instructor the other day and asked him this question. He told me that I could go for smaller projects, but I m starting a chess program and I was wondering what Stack Overflow thinks about this issue. Should I include all headers into one file, or separate them?

最佳回答

Normally, you want separate headers.

Including more than necessary does a few potentially bad things.

  1. This is single greatest cause of slow compile times. Unnecessary inclusion of extra headers slows down compilation, since each source file has to worry about more info than it needs. It starts as a small problem, and before you know it hundreds of developers are wasting dozens to hundreds of hours each because the problem got too far out of hand to fix. Even though you re working on small problems, it s important to understand proper separation of headers - it s easy to get right the first time but very hard to fix later if you ignore it.

  2. You lose the ability (depending on compiler/IDE) to correctly handle dependencies, and wind up building more information than you need to build.

  3. You lose maintainability. It s more difficult to maintain software when you have large, single files, than when you have lots of small, concise files.

  4. You make the code more difficult to understand, hence more prone to bugs. By having everything in one file, it s easier to get "lost" in the huge header.

问题回答

There are some times when this practice is useful:

  1. Pre-compiled header files with code that really doesn t change that often.

  2. APIs

There s no one-way-fits-all solution. Arrange your headers how it makes the most sense. 90+% of the time, that means that each module includes only the headers it needs. However, some programs have little isolation of the data structures between modules—that could mean that there is a use for

#include "alltheheaders.h"

For a smaller project you ll never notice the difference. However, once your project reaches significant scale, stick by the old adage, "Only reference headers in files that are directly using them". If you have myclass.cpp and myclass.h, and myclass.h doesn t directly need a specific header, reference it in the cpp.

This may take more time, but best practice is always worth the time.

I just want to add that, yes this is normally a bad idea, but it can be useful on occasion. Never for entire projects though.

For example, I recently wrote a utility to perform a stack dump on windows. There were 4 common headers I was going to include, so I made (in a detail folder, a convention I stole form boost use) a windows.hpp, which included those four. The implementations could then use that header to easily get the necessary functions.

While it might not carry the same weight as a mega-includes-480-headers header, it was a grouping of a handful of common headers, and it was quite helpful. the key thing here is it was a small collection of related headers, used in a portion of the code.

It s a bad idea in general, because you can have dependencies you ll not be able to solve in this case.

But it s usually a good idea to create a header file with all base headers you are using from different libraries or your own header (with structs and data types definitions).

You should keep the number of headers included in any source (*.cpp, *.cc etc.) file to the minimum required to compile that file. Including extra headers will increase compile times. You should also try and reduce the number of includes in your headers - you can do this by forward declaring classes rather than including their headers.

e.g. The following code has an unnecessary include in the header

Example.hh

#include "SomeClass.hh"

void SomeFunction(SomeClass const& obj);

Example.cc

#include "Example.hh"

void SomeFunction(SomeClass const& obj)
{
   // code here
}

It can be written as to move the include from the header to the source file

Example.hh

class SomeClass;

void SomeFunction(SomeClass const& obj);

Example.cc

#include "Example.hh"
#include "SomeClass.hh"

void SomeFunction(SomeClass const& obj)
{
   // code here
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签