This question already has an answer here:
I know that it is common in C/C++ projects to place header files in a directory such as include
and implementation in a separate directory such as src
. I have been toying with different project structures and am wondering whether there any objective reasons for this or is it simply convention?
Besides (arguable?) usefulness for keeping things orderly, useful in other projects etc, there is one very neutral and objective advantage: compile time.
In particular, in a big project with a whole bunch of files, depending on search paths for the headers (.c/.cpp files using
#include "headername.h"
rather than#include "../../gfx/misc/something/headername.h"
and the compiler passed the right parameters to be able to swallow that) you drastically reduce the number of entries that need to be scanned by the compiler in search of the right header. Since most compilers start separately for each file compiled, they need to read in the list of files on the include path and seek the right headers for each compiled file. If there is a bunch of .c, .o and other irrelevant files on the include path, finding the includes among them takes proportionally longer.I prefer putting them into the same directory. Reason:
The interface specification file(s), and the source file(s) implementing that interface belongs to the same part of the project. Say you have
subsystemx
. Then, if you putsubsystemx
files in thesubsystemx
directory,subsustemx
is self-contained.If there are many include files, sure you could do
subsystemx/include
andsubsystemx/source
, but then I argue that if you put the definition ofclass Foo
infoo.hpp
, andfoo.cpp
you certainly want to see both of them (or at least have the possibility to do so easily) together in a directory listing. Finding all files related tofoo
Finding all implementation files:
Finding all declaration files:
Simple and clean.
Convention is one of the reasons - most of the time, with effective abstraction, you only care about the interface and want to have it easy just looking at the headers.
It's not the only reason though. If your project is organised in modules, you most likely have to include some headers in different modules, and you want your include directory to be cleaned of other "noise" files in there.
Also, if you plan on redistributing your module, you probably want to hide implementation details. So you only supply headers and binaries - and distributing headers from a single folder with nothing else in it is simpler.
There's also an alternative which I actually prefer - public headers go in a separate folder (these contain the minimum interface - no implementation details are visible whatsoever), and private headers and implementation files are separate (possibly, but not necessarily, in separate folders).
It keeps your folder structure cleaner. Headers and source files are distinctly different, and are used for different things, so it makes sense to separate them. From this point-of-view the question is basically the same as "why do source files and documentation go in different folders"? The computer is highly agnostic about what you put in folders and what you don't, folders are -- for the most part -- just a handy abstraction because of the way that we humans parse, store, and recall information.
There's also the fact that header files remain useful even after you've built, i.e. if you're building a library and someone wants to use that library, they'll need the header files -- not the source files -- so it makes bundling those header files up -- grabbing the stuff in
bin
and the stuff ininclude
and not having to sift throughsrc
-- much easier.In short, a few reasons:
Have a look at the article Organizing Code Files in C and C++ which explains it well.