r/C_Programming • u/Big-Rub9545 • Oct 31 '26
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.