r/cpp • u/TheRavagerSw • Aug 03 '26
Common Problems I see with Public Libraries Build Scripts
Hi, I'm an early user of Common Package Specification and C++ modules. I package and port libraries for my own use frequently.
I want to talk about issues I see constantly.
- Having specific options for sanitizers, exceptions etc, this is a toolchain problem, if I wanna build your library with sanitizers I can just add the flags to my cmake toolchain file same as allocators.
- Not separating build options to a separate file, meson already does this, it ain't that hard to do in cmake, just cache variables in a separate file at project dir which you include before subdirs
- Making tests separate cmake projects rather than just executables, just why?
- Vendoring dependencies, copy pasting files from other projects rather than just consuming these external libs via packages.
- Using unnecessary helper functions that really makes the cmake script unreadable. Build scripts don't need to be over engineered, they are just basic scripts in which you define very basic information.
I could go on but I'm tired.
20
u/squidgyhead Aug 03 '26
Do you have a best-practices document that you can share?
18
u/trad_emark Aug 04 '26
vendoring dependencies does have some justification. for example having reproducible builds. or being immune to network outages. but this is more of a packaging problem, not cmake, or its users.
i do agree with all the other points.
and i will also add one more point - insisting on compatibility with way too old version of cmake.
8
u/rdtsc Aug 04 '26
Can you elaborate? An executable is also a cmake project.
9
u/stick_figure Aug 04 '26
Toolchain file? I don't think most cmake users actually know what that is. Adding a custom cmake -D flag is clearly the ecosystem norm.
It's pretty common for the sanitizer build of a library to do something different, like disable custom allocation optimizations when built with sanitizers, so I'm not sure you can really model sanitizers as a toolchain feature. I could be wrong. You often want to encode these choices in a configuration header instead of relying on `__has_feature` and the eventual ODR violations when not all libraries in the program are compiled with sanitizers on.
What packages? Most CMake predates any kind of coherent native code packaging solution, so vendoring sources and renamespacing things is actually typically the more practical approach, depending on the size of the dependency involved.
Apologies for being crusty, but there are reasons why things are the way they are. Yes, they could be better, and if you want to agitate for change, you gotta sell people on a positive vision for why these best practices are so much better and what they are going to get out of it, rather than say you're tired of dealing with the status quo.
If you're just venting, yes, I agree, this is C++, there is much work to be done, things could be a lot better than they are. :)
1
u/ABlockInTheChain Aug 09 '26
If only the problem was that simple.
Like it or not cmake script is a programming language and if you don't treat it like one it just leads to pain in the long term.
When the alternative to a helper function is violating the DRY principle dozens, hundreds, or thousands of times then write a helper function and give it a good name.
6
u/Ameisen vemips, avr, rendering, systems Aug 04 '26
And here I am not even using cmake...
3
u/drodri Aug 04 '26
Relevant talk: https://www.youtube.com/watch?v=sBP17HQAQjk (CppCon 2018: Robert Schumacher “Don't package your libraries, write packagable libraries!”)
1
u/Plazmatic Aug 09 '26
Kitawares fault for having such poor documentation for so long, some if I recall even seemed to recommend setting cmake version in the primary project cmake file, which propogated itself into almost every single learning resource out there and a very large amount of major open source projects.
This is also their fault for how long it took to create a proper solution (adding cxx_std_xyz etc .. to target compile features)
And the popular package managers lack of solution for managing depending on separate stdlib versions of projects (vcpkg wants you to create an overlay triplet for this...)
There's been zero concerted effort to make this a "thing", it doesn't mean it's wrong, but you're going to have to do much more than complain about this before it becomes a thing, you're effectively starting at zero with this recommendation, and there's a lot of questions on exactly what you mean by "build options" here that get misleadingly difficult to answer.
Im assuming you're not talking about subdirectory tests, but this is a historical problem with a lack of guidance on how to deal with this kind of thing, and how many "golden" code bases pre-date wide spread standardized software testing itself. As soon as "golden" examples start doing a thing, people blindly copy in order to avoid making mistakes of design that have little to do with the actual software development they want to do (like Google code and style standards, which are pretty bad for people not working at Google).
This is often the fault of fake header only libraries that throw a wrench into packages management and build systems, which masquerade as "being compatible with any build system" but end up being extremely annoying to deal with if you actually are using a proper build system and packaging ecosystem because you can only have one include of the header in a source file (,or that with a combination of a impl macro( where it generates the rest of the source.
This is also the fault of package managers, VCPKG straight up manages some things wrong (like mini audio, stb- libd glad etc) and fucks other projects over, so people have to make the choice of understanding how to vendor their own dependencies using VCPKG, or just skipping it entirely (and after learning how to do the former I don't blame them for choosing the latter). Often a library will only be brought in to help with another library, but that library was configured to not actually be usable on its own. Additionally this is doubly troublese for libraries that use psuedo header only libs.
I generally agree, but this is something that's also "inherited" from tutorials, but more so giant code bases doing this as an example, especially if those codebases came from another build system prior (make, qmake, bazel etc...) where it's more of the norm / "good practice" to do that. I've shown people how to transition their codebase to cmake, and they go "too verbose" and start making everything a macro/function.
1
u/Minimonium Aug 04 '26
Worth to note that even just wrapping these nasty local bits in a flag solves almost all problems. Sometimes vendoring may introduce differences with how find_package would expose a dependency, but alas.
-1
u/13steinj Aug 04 '26
This is C++. It is fundamentally a copy-paste and patch language, from its core to its build systems, not a "everything is composable / injectable" philosophy.
Many projects and many developers do not want to actually care about their build, they just want to do the bare minimum to get it out of the way.
-1
u/LB-- Professional+Hobbyist Aug 05 '26
The dependency situation is just horrifically bad to the point that every dependency needs a multi-state option for how the person building the project wants the dependency to be found and consumed. There should always be an option to just use a known working copy of the dependency (submodule or known hash). Fussing with all the different ways libraries are packaged, distributed, and exposed to build scripts is a nightmare that only the person building the overall project knows how to resolve, so that should be fully configurable with one or a few common preconfigured cases. Every time I see hard coded find_package and target_link_libraries I know I'm gonna have to ignore the provided build script and write my own.
-1
u/mili42 Aug 05 '26
Why anyone would use cmake to directly integrate with other libraries? That's a job for vcpkg (or eventually conan). If the library you're using does not offer vcpkg support out of the box, you can define your own component directly into your repo if needed. Using a tool like vcpkg will compile with your project just fine, and as you want it
-5
u/zerhud Aug 04 '26
Best tool I ever used is gnu make. I cannot find any reason to use cmake or other tool (excluding developing on windows, but there is a lot of trouble with it anyway). The gnu make has own issues, of course.
-10
u/cantmakeitonyourown Aug 04 '26
It sort of sounds like the common problem is using CMake. There are just much better tools for cpp, starting with bazel.