r/C_Programming • u/Big-Rub9545 • 28d ago
Handling personal libraries
As seems to be a rite of passage for many C users, I've written many of own mini-libraries to handle things that are missing from the language and/or standard library (the usual: useful macros or constants, data types like strings or dynamic arrays, type aliases, etc.). I've been focusing more and more on integrating these into current projects instead of hand-rolling slightly different (and possibly incorrect or incomplete) versions each time.
However, I've had trouble keeping all of this organized, since these libraries also get updates or fixes when I feel the need to make them, forcing me to go through a number of projects to update header or source files (and possibly replace some pre-built library files as well). Unlike with most external dependencies, I can't throw a package manager (as evil as they already often are) at the problem, and I do pay attention to keeping the version of these mini-libraries up to date in my projects.
The ideas I've had thus far:
- Static library with headers (similar to the way the standard library works). This is great but it still means I need to keep the header files and library file(s) up to date. I also need to recompile the library before I can distribute it across projects.
- Independent, header-only libraries. These could work very well (albeit with some not-so-clean macro flag tricks to avoid ODR issues), and they avoid the dual handling needed if I have both headers and source files. I'm also already often updating headers, so might as well make that be the only change needed. However, still has the issue of needing multiple updates.
- A single, header-only library. This effectively reduces the problem significantly at the cost of my own convenience. I can move around a single, fat header file and update it as needed with minimal work (if in doubt, just copy in the latest version and ta da). However, editing or working on it becomes somewhat of a nightmare. I'd be looking at possibly thousands of lines working on entirely independent features. It would also (even if only in one source file) mean compiling tons of code the project may only need a very small fraction of. Compile times are rarely an issue for me with C projects, but it's something to consider nonetheless.
Appreciate any thoughts or suggestions on this. I have a feeling this is somewhat of a trivial issue (it often is with external libraries, just not my own private ones), but I can't find a very good solution for it that makes my life easy.
12
u/bullno1 28d ago
8
u/ChickenSpaceProgram 28d ago edited 28d ago
I just make my library a git submodule usually, it makes updating easier but still keeps the updates under my control. Occasionally if I only need a feature or two I'll copy headers around manually, or even just rewrite the code completely.
I've also in more recent projects started using intrusive linked lists more heavily. They're easy to write, and they mean I don't have to have a "datastructures library"; I fit my datastructures to my problem instead of the other way around. That's not to say eg. arraylists aren't useful, but usually I find I need to either string a few structs together (in which case linked lists backed by an arena are fine) or I want a character buffer anyways so it's fine if it isn't generic.
8
u/septum-funk 28d ago
I use CMake and its built in feature FetchContent that essentially clones a repo and adds to a _dependencies folder under build. From there it's a CMake target you can link to any of your others as if it was loaded any other way. From there you just commit and push your changes and CMake will grab the new version on next build
If you don't use CMake this doesn't apply, but honestly I don't see much reason not to at this point if you're already proficient enough to be using your own libraries. Some say CMake is overcomplication, but I just find it incredibly useful for automating builds exactly the way I like them with good editor integrations
2
u/catbrane 28d ago edited 28d ago
Don't do header-only for C, they don't work very well. And you'll still need to handle updates by hand.
IMO this is best done by your build system. Have a git repo with your utility library in, then add it as a dependency to projects that use it. When you build a project, your build system will automatically fetch the correct version of your util library for you.
If you use semantic versioning for your util lib, the build system can check things like ABI and API compatibility for you.
meson has a nice subproject feature -- you write a tiny wrap file for a dependency (just a couple of lines) and if it's missing it'll add it to your build. cmake has a similar feature, but not as nice (imo).
https://mesonbuild.com/Subprojects.html
1
u/WittyStick 28d ago edited 28d ago
If you use autoconf & automake, you could potentially use
gnulib-toolfor this. It supports a--local-dirargument which can contain your small projects implemented as gnulib modules.To set up a new project that uses one or more of them:
To add a new module to a project
And to update the modules if you have made changes to them:
Gnulib will basically copy the relevant files into your project directory and update your automake.am etc.
some/dircan then be a central repository of your smaller libraries that are trivially reusable. If any of your modules depend on others, you can specify this and gnulib will also import the dependencies.There's a huge number of modules already supplied with gnulib that you will find useful anyway, and if you have anything that's genuinely useful to many others, you could push it upstream.
1
u/21Ali-ANinja69 27d ago
I mean, you could put your libraries in a separate repo, and then just symlink to them. Then any change to the original reflects everywhere, and anyone who clones it doesn't have to worry about submodules or whatever
1
u/actguru 27d ago
Why not write a small utility program that combines your headers into a single header where you edit the small ones and generate the fat header.
1
u/not_a_novel_account 27d ago
Why not? Just package them.
A vcpkg portfile for a simple static library is like 10 lines, a conan recipe is about the same.
1
u/lmemsm 27d ago edited 27d ago
I have a lot of custom libraries I use in my own projects as well. I do use my own build system/package manager so I can track which programs use which libraries and update a library's files when I need to. I can use it in my home directory or wherever I set it up on my system. I'm not a fan of single header libraries. Take nanosvg for an example. FLTK, sdl2_image and my build of mupdf all use it. Each project includes their own version, so if something goes wrong with the code, I have multiple places I may or may not need to fix it. I'd rather have one system wide library and update everything in one place. Building applications statically and using static libraries does provide one advantage. If I update a library and break ABI, the programs using the library keep working. It does mean I have to recompile each program eventually to test that it works with the latest version of the library. If I use a shared library and don't break ABI, everything should theoretically still work fine, no recompile required. However, that leaves checking all the programs that use the library at some point to ensure that is truly the case. For me, using a library is preferrable to maintaining pieces of the same code in each of the projects separately. However, the more programs you build, the more you have to maintain.
1
u/darthbane123 27d ago
Git submodules are the way. Makes it really easy to pull in what you need and sync for updates as needed.