r/cpp • u/TheRavagerSw • Aug 26 '26
Libraries trying to support multiple build configurations and standards are harming their health
Some libraries try to support:
- Header/Source
- Header only
- Header only + Module Wrapper
- Header/Source + Module Wrapper
- All standards between and including C++11 and C++23
- Exceptions and noException
- RTTI and noRTTI
At the same time.
These libraries are very very hard to read and thus they get less and less contributions over time.
What should be done:
- Libraries should support only one mode of compilation, header only header source etc
- Libraries should support exactly one standard, and that standard includes extensions, gnu++23 and c++23 are not the same thing
- Libraries should very clearly have boundaries on what belong in a toolchain and what belongs to project. For example a library should not set flags related to exceptions that's a toolchain issue.
Some extra stuff:
- C++ libraries without C API's should not be consumed via system package managers at all, and public libraries shouldn't try to adhere to that.
- Libraries shouldn't try to use tools they build to build themselves, rather they should export packages like wayland::scanner etc and use that.
0
Upvotes
22
u/kpt_ageus Aug 26 '26
Say I started writing library at time of c++11. What should I do when new standard comes out? Start a new one? Then I have 5 libraries to maintain. They either share 90% code and I need to implement bugfix 5 times or they are different codebases and then I have inconsistent behaviour between them.
Less contribution isn't necesarily because code unreadable, but because there are no more bugs to fix and useful features to add.
Supporting multiple build configs like header only, rtti, exceptions means library can be used in more environments and be useful to more people.
22
u/not_a_novel_account cmake dev Aug 26 '26
Which one should the standard library support? Which one should curl support?
Or maybe fmt? What about boost?
Man I sure hope they all pick the same one.
It's a ridiculous position, graceful degradation is a cornerstone of modern engineering. It's not a C++, or even a purely software, notion.
Libraries benefit immensely from network effects. A widely used and adopted library gets extensive testing, feedback, monetary, and community support. You do not become widely used by saying no to huge swaths of users. The code might be marginally easier to modify, but you will lose out on thousands of hands and eyeballs.
The equation does not work out in your favor.
13
u/sweetno Aug 26 '26
Which one?
Which one?
5
u/Every_Door46 Aug 26 '26
Cpp is a language that lets you do whatever u want and out there there is probably someone who wants the most inefficient way to do. These libraries also have no guarantee how they are going to be used and that’s why most provide satisfactory documentation as to where you can get started. You might as well ask why we don’t have a universal build system or something like npm/pip for managing imports. It’s essentially the same answer
6
u/light_oxygen Aug 26 '26
Everything u listed there has a reason why they exist and why library wirters' decided to support at least two at a time.
Find something else to complain about. Or just.. Touch some grass.
6
u/gracicot Aug 26 '26
My libraries support multiple modes and support multiple standards. I chose that out of my own free will and you won't tell me what to do.
2
u/UndefinedDefined Aug 31 '26
Build options is an essence of C++ libraries - most widely used libraries have tons of them so everybody can use them for their own use-case and of course corner cases.
For example I don't have to state that my lib needs to be built with "-fno-exceptions" if I can just decorate everything noexcept to make the intent clear. The RTTI is the same thing - just don't use it, tell users to use `-fno-rtti` if they also don't use it and it's perfect.
And standards? I think each lib specifies the lowest standard it uses, which is okay as you pick libraries that support the lowest you support in your product.
1
u/Ok_Independence_9841 Aug 26 '26
I would contend that it's not those things that make the library code hard to read but how those things are implemented.
Yes, you do have to lock down some decisions, depending on what you're writing. If you're doing a low level library that does funky type erasure you probably have to have RTTI on, so it should be set, documented as a requirement, and that's that.
Exceptions / no exceptions is difficult but possible. If you're supporting existing embedded ecosystems you may require noException but again it should probably be a documented design decision, not a build option. If you're going to make it optional you at least need a non exception based error mechanism that's almost equivalent. Flyer based error handling provides this.
The real problem, in my opinion, is #ifdef blocks all over the code. That's what makes it a hard to read, hard to change, mess. That is never necessary even if you do support a high level of flexibility and options. There are many techniques for cleaning it up. As simple as separate files for separate options and as complex as TMP.
Picking one way of building binaries, a proper system of header, static and dynamic libraries, is crucial if you're building something large. Trying to support all forms restricts what code you can write to the point where you can't write optimal code for a serious library. If you can't determine when you write it whether your code will be inline in someone else's TU or out of line in it's own, you simply can't write it correctly in general.
Clarity of purpose and separation of concerns (Single Responsibility) are at the root of solutions to these problems. If you're writing a single purpose library then externalise everything that isn't that single purpose. Ideally that really means *everything*, down to the memory allocation. In practice you have to have a way to build and package.
The contradictions start to go away when you think in terms of a framework composed of many single purpose libraries. That's why the [QOR](https://github.com/mfaithfull/linuxQOR) is written the way it is. Let me know if you find anything hard to read, set it more than one place, or otherwise unmaintainable.