Separate "include" and "src" folders for application-level code?

StackedCrooked picture StackedCrooked · May 27, 2010 · Viewed 27.5k times · Source

This questions concerns mostly Unix/Linux style C++ development. I see that many C++ libraries store their header files in a "include" folder and source files in an "src" folder. For the sake of conformance I adopted this in my own code. But it is not clear to me whether this should be done for application code as well. I've seen a few cases where a flat directory structure is used for that. What would be the recommended approach?

Answer

Patrick picture Patrick · May 27, 2010

I also separate them, but not strictly on the extension, but on the access of the file.

Suppose you have a module that manages customer information and uses 2 classes to do this: Customer, CustomerValidityChecker. Also suppose that other parts in your application only need to know about the Customer class, and that the CustomerValidityChecker is only used by the Customer class to perform some checking. Based on these assumptions I store the files like this:

Public folder (or include folder):

  • customer.h

Private folder (or source folder):

  • customer.cpp
  • customervaliditychecker.h
  • customervaliditychecker.cpp

That way, it becomes immediately clear for callers of your module which parts are accessible (public) and which parts aren't.