r/cpp • • Sep 20 '26

Self Contained Source Groups are amazing

I basically adopted this idea from Abseil (https://abseil.io/) and from this CppCon talk: https://youtu.be/re4Oy1IVj-s?si=3bwtKqOyMAX6WSAs.

I structure each source group in my projects like this:

src_grp/
├── iface/
│   └── smth.cppm
├── impl/
│   └── smth.cpp
├── inc_pub/
│   └── pub.hpp
├── inc_priv/
│   ├── priv.hpp
│   └── maybe.inc
├── test/
│   └── test.cpp
├── bench/
│   └── bench.cpp
└── docs/
    ├── some.md
    └── doxygen.in

I strongly recommend this approach over older project-wide conventions such as having a single global test/ or bench/ directory etc.

0 Upvotes

16 comments sorted by

View all comments

1

u/UndefinedDefined Sep 25 '26

I honestly never understood why I would need so many directories to describe source files that only make sense to be together. It's 2026 now, you can use your build system to distinguish between headers to install and not - for example Qt project uses `_p.h` suffix to make a header private, and build system can deal with that (a simple regex match).

1

u/TheRavagerSw Sep 25 '26

You should always use explicit files when writing build scripts. Reflex match never works out

1

u/UndefinedDefined Sep 26 '26

That most projects use, so why the separation? It just makes no sense to have a directory "inc_priv" (and I don't talk about the horrible name). Most projects I worked with either use "src" and "include" or just put everything together as it belongs together. And what you install is public headers, that's it.

1

u/TheRavagerSw Sep 26 '26

You need to install private headers sometimes, for example when using C++ modules

1

u/UndefinedDefined Sep 26 '26

Then they are not private anymore.

0

u/TheRavagerSw Sep 26 '26

They are private, you need them when building your C++ module interface files, they don't get used by the end user.

1

u/UndefinedDefined Sep 26 '26

It makes no sense to call them private then. Private means the user doesn't need them, and doesn't need transitive dependencies these headers depend on.

Just don't call them private headers then. They are not private if user needs them for whatever reason.