Source Header separation
Hi all,
Source and header in the C times were directory separated because you could ship a binary library and the headers to use it.
WHy so many people still segregates C++ headers in different directories even if a lot of the code is nowadays in the header files ?
14 comments · 32% upvoted · other discussions
Do you mean “why do some libraries still ship with headers in a
/includedirectory and source files in a/srcdirectory?That’s because how you compile things still hasn’t changed; you need to add headers to include from external libraries on a search path in your build system, and unless you use something like bazel, this is often easiest if you just have a single top level directory you can stick on the search path. And many C++ libraries still have source files. Putting absolutely everything into headers is an active anti-pattern at any kind of significant scale because it makes compile times horrible.
If you mean “why do people organize the headers/source into a taxonomy,” it makes discovery within the library easier when browsing source or typing include paths. In a repository that may provide multiple components which can be used individually, it also provides semantic grouping into those components.
peppedx [OP] 2 points · Nov 2025
I see many non library projects with private implementation headers put in include directory. I do not see why. Obviously public headers for library are a different things...still
aruisdante 1 points · Nov 2025
Private implementation headers still have to wind up on the search path if they are included from public headers, which most wind up being. It’s usually easiest to just not risk it and put all the headers in the header export, and use other mechanisms to control visibility for external use. For example visibility rules in your build system if it supports it, and/or physical structure rules like using
detailfolders and namespaces.Wooden-Engineer-8098 1 points · Nov 2025
your last paragraph is wrong, it makes discovery harder because you have to jump between directories. when header is located adjacent to source, it makes discovery easier
aruisdante 1 points · Nov 2025
I agree, I don’t think headers and source files should be split. My last paragraph was referring to a taxonomy within a source tree independent of the
srcandincludesplit. So:|- component_a/ |—— detail/ |—— tests/ |- component_b/ |—— detail/ |—— tests/ |- component_c/ |—— detail/ |—— tests/Instead of just having everything in a single giant folder.Note I’m similarly against making
testsa top level taxonomy, as it makes finding the tests for a component really difficult. It should be a taxonomy exactly at the level it’s testing, sofoo/testsshould contain the tests forfoo.