7 ms·
Single-file cross-platform C/C++ headers implementing self-contained libraries
- paulddraper 8y agoIt's a sad truth of the state of C/C++ build systems that header-only and single-file libraries (which take very long to compile) are so sought after.
- ioquatix 8y agoI agree with you. Actually, we've been having a lot of success developing an in-house tool to solve this problem and it's helped us build agile modular systems using C++. https://teapot.nz https://teapot.nz It's a one stop shop for building modular C++ applications with an opinionated set of packages here: https://www.github.com/kurocha https://www.github.com/kurocha That being said, `teapot` doesn't have any default build rules, it relies on packages to provide them, so you could build an entirely different eco-system of packages with minimal effort.
- sebazzz 8y agoIt can be mitigated a bit by using precompiled header files.
- jchw 8y agoI highly recommend you give Bazel a look if you haven't. I'm coming from a biased perspective, since I work at Google where we have a nice build setup. But, I like Bazel so much I use it and hack on it in spare time. There's really only two things that bother me: - Windows support is alright but it's a bit weird and wants some Unixy stuff to exist. It's being worked on in small bits but it could be better. If you don't care about this you'll have a good time likely. - Sadly it isn't widely used outside of Google, so you're mostly on your own. The only upshot is that the Starlark language makes it really easy to compose BUILD files, so I don't find it too difficult to manage a few BUILD files for things like SDL2. Unlike a lot of other build systems, like CMake for example, it would be much less hassle to consume other Bazel projects directly, since it's designed to work that way, so if it ever did gain widespread adoption it would be a really nice solution. There's definitely some things that would turn off the average user. Like the BUILD files, in order to be predictable, contain dependency information, which either needs to be updated by hand or tools (you can guess which is preferred.) On the other hand, builds are very predictable, and it works such that you can easily express dependencies across different programming languages and even autogenerated code from another target. Nothing is perfect, but I find Bazel is super nice for C/C++ compared to the alternatives I've used. It's a different way of approaching the problem of building software, one that I've found very novel despite the shortcomings. I'm worried it's too easy to miss what makes it special behind some of the turnoffs people initially hit.
- misnome 8y agoHow does this solve the build time problem the parent post was lamenting? I’ve looked a little bit at bazel but haven’t spent long on it. It seems to really, really want you to do things in its specific googly way ( that seems to stem from monorepo style and always knowing the exact build path and never needing to distribute to users) which is hard if e.g. trying to to convert an existing project that doesn’t perfectly fit (us: combined C++/Python with some legacy fortran generation) Or a project that has external dependencies currently being chain-built but isn’t itself built with bazel (e.g. most the planet) - we don’t all have dedicated build teams to accurately and bug free reproduce other libraries build procedure (also getting away from a buggy reproduced build is part of my system rewrite intention). I seem to remember the documentation also had the usual problem that the easy examples were well documented but hard to tell how to do anything more complex, but maybe I didn’t dig enough. CMake has a similarish problem except the problem is finding more modern ways of doing things that don’t suck (I’m aware of all the talks about this, many of them are big on ambition but low on details, and usually just teach transient dependencies).
- misnome 8y agoIgnore the first sentence, I think I misread what you were specifically commenting on for that.
- jchw 8y agoIt improves the build time problem by offering a way to do fast, hermetic builds. Caching is very accurate, targets only rebuild exactly when they need to, and you can distribute just about any kind of work over a build farm. It may not sound like much, but having the distribution at the build system level, versus say, distcc, makes a big difference, especially if you have long build steps that aren't just compilation. You literally can't accidentally have dependencies that aren't explicitly defined. When you go to compile it will not find headers from targets that are not depended on, for example. >I’ve looked a little bit at bazel but haven’t spent long on it. It seems to really, really want you to do things in its specific googly way ( that seems to stem from monorepo style and always knowing the exact build path and never needing to distribute to users) which is hard if e.g. trying to to convert an existing project that doesn’t perfectly fit (us: combined C++/Python with some legacy fortran generation) I don't really think this is the case. There's definitely certain aspects that can be challenging but I do not feel it has much to do with monorepos. It works well with a monorepo using the workspaces system, but it also should work fairly well using modular git repos as well, each repo is just treated as it's own workspace. It's possible to even have Bazel go and get other workspaces, and you can refer to targets inside of other workspaces. One of my favorite parts about Bazel is combining different languages. If Starlark rules for the language you are trying to build for don't exist, you can write them. Starlark is a fair bit like Python, it's relatively easy to learn and write your own targets. I acknowledge that most people don't want to maintain BUILD files for external stuff. There are some options here. Some people already maintain this, like here: https://github.com/bazelregistry https://github.com/bazelregistry - and there are experimental rules for dealing with external build systems. I use it outside of Google on personal projects and find it perfectly manageable. You don't really need a large build team unless you're doing interesting things. There are a few aspects of Bazel that really are tough, like how you must explicitly specify dependencies on every target, and how actual builds can't even see files not specified in dependencies, and when you get fairly deep you'll probably run into trying to understand what's going on with runfiles. It's definitely a bit more compliant than a Makefile generator at this point. But in exchange, it delivers on its promise of being fast and correct.
- Fronzie 8y agoI'm wondering if there's still a good reason. Cmake with either vcpkg or conan makes for a very reliable system. The syntax still isn't the prettiest, but if done well ('modern' cmake, using targets), cmake scripts are very short, so it's still worth it.
- IshKebab 8y agoThe trouble is not everyone uses CMake or vcpkg or Conan. As a library author I'm not going to provide build scripts for every C++ build system in existence. The beauty of a single header include is that there is no build system, so I don't have to provide any scripts at all. It's an unfortunate solution but I can see why it is so popular.
- int_19h 8y agoThese are kinda misleading - "single-file" is a technically accurate term, but if you think that implies that you can just #include them, it's not that (i.e. not what is normally called "header-only"). It's basically a header and implementation combined into a single file, with the implementation part #ifdef'd. So you need manually instantiate that implementation in one of your existing translation units, or add a new one specifically for that purpose. At that point, I don't see how this is preferable to just having an .h/.cpp as usual, and zipping it for distribution purposes.
- pmarin 8y agoless messing with build systems.
- megous 8y agoIt looks like every chnage will also lead to recompiltion of all dependent files. It seems quite inefficient.
- flohofwoe 8y agoSuch library-style headers that you drop into your own project are usually never changed. You can also group the stable headers into a different implementation source file from the headers that change frequently.
- flohofwoe 8y agoThey're also known as STB-style headers, but IMHO all those names are correct. One advantage is that you don't need to mess with build systems for configuration, instead put the configuration defines before the implementation right in the source code. Another advantage is potentially faster compilation if you put many implementations into the same implementation source. Same idea as unity-builds basically. Even though you're using dozens of header-only-"modules", the compiler only builds a single file.
- quizotic 8y agoDon't C++ 20 modules solve this problem?
- AJRF 8y agoI write C for a living but i'm not terribly experienced. I was wondering is there a reason C and C++ code favours comments over descriptive names? I'm almost certain i'm not making this up, but go into any random file here and look at the names. Structs will have members called p with a comment saying "position". It seems very silly.
- MaulingMonkey 8y agoSome old C code was written on monochrome 80x25 ttys. Or written by mathematicians who will be confused why you haven't simplified "position" down to "-p(o2)stn", because clearly "position" is multiplying 8 variables together. Or typed on a broken keyboard, hence "creat" I assume. Or written on programmers who remember, or were still using, punchcards. And maybe have some RSI and no IDE autocompletion to help. And of coure any new C code would look out of place if it didn't follow these old shibboleths... better make new C code an unreadable mess too, no? ;)
- eru 8y agoNot all of math is done in commutative rings.
- enriquto 8y ago> Structs will have members called p with a comment saying "position". It seems very silly. It does not seem silly to me. Coming from mathematics, single-letter variable names are the only acceptable possibility, especially in numerical code. I tend to read contiguous letters in a formula as products. Besides, descriptive variable names violate the principle of "don't repeat yourself". It is better to describe the variable only once, in a comment next to its declaration, instead of several times. Programmers should understand that not everybody who programs has a background in computer science, and certainly the traditional conventions of computer science are not universal and not generally reasonable. Many of us have a scientific background, where descriptive variable names are silly. We do not want our numbers to have meaning, that's the whole point of abstraction. Thus, they are named x, y, z, and so on.
- bla3 8y agohttps://github.com/nothings/stb https://github.com/nothings/stb is another set of good header only libraries. (I'm not the author and don't know them, I'm just a happy user.)