News for hackers. Stuff that matters.
Re:Single_file_libs: List of single-file C/C++ libraries by flohofwoe on Monday October 26, 2020 @06:02AM (#24894440)
Single-file-libs done right have the implementation in a separate #ifdef/#endif block. They don't pollute your code any differently than a regular .h/.c setup would (at least outside that one source file which includes the implementation).
Re:Single_file_libs: List of single-file C/C++ libraries by huhtenberg on Monday October 26, 2020 @06:32AM (#24894608)
> They don't pollute your code any differently than a regular .h/.c setup would.

For that to happen you'd need to have a separate .c to include just that .h with IMPLEMENTATION defined. So in the end there's still an .h and a .c, except the .c is now completely superficial.

Re:Single_file_libs: List of single-file C/C++ libraries by flohofwoe on Monday October 26, 2020 @06:36AM (#24894635)
Yeah I just added that while you wrote your comment, sorry.

You can still put multiple implementations into the same source file and compile that into a library, for example:

https://github.com/floooh/sokol-samples/blob/master/libs/sok...

That's what I usually do for projects that don't just consist of a single source file (e.g. simple command line tools).

An actual advantage of this approach is that you can add any configuration defines in that same source file, instead of passing them in from the build system via compiler command line args.

Also for bigger projects you can have dozens or hundreds of header files but only a small number of source files (e.g. one source file per "system" or split by change frequency, or by any other criteria (like whether the implementations include Windows.h or other system headers). A small number of compilation units means fast compile times (since it's essentially a unity build), but at the same time you have enough control over the project's file structure to balance compile times vs "namespace pollution" vs what changes trigger a rebuild.

Re:Single_file_libs: List of single-file C/C++ libraries by hburd on Monday October 26, 2020 @11:14AM (#24897031)
You can avoid the .c file by building the .h and passing the compiler a -DIMPLEMENTATION option.