
Single-file C/C++ public-domain/open source libraries with minimal dependencies - brakmic
https://github.com/nothings/single_file_libs
======
SwellJoe
I've lately found myself wondering why there isn't a C/C++ tool comparable to
npm, cargo, cpanm, etc. There's more C/C++ code in the wild than most
languages, but it's not easy to find, not easy to install, not easy to update
(though that's a problem in the npm ecosystem, too), and not easy to
integrate. A good package manager would go a long way toward modernizing the C
ecosystem, I think.

~~~
midita
coz c++ itself is architecture dependent. and even lots of c++ code are
architecture dependent. c++ has much longer history than js or java. It runs
on bare metal machine for all kind of purpose. So it is much harder to write
universal useful libs.

~~~
djsumdog
Totally. When you consider linking and binary compatibility, C/C++ does well
with the Linux package management world that's grown up around it.

Package management is somewhat less useful modern code that tends to self
contain things (Java/Groovy/Scala) or where you want to pin dependencies for
stability and create unique environments for them (Python's virtual env,
Ruby's RVM, or .. and this is kinda a stretch, but Docker containers).

------
kevinoid
This is a great list, it looks useful!

In addition to clib[1] mentioned in this list, there's also CCAN[2]. It's
another collection of small C libraries, but unlike clib and this list, the
libraries are maintained as a single project (for better or worse).

I'd love to see some more convergence on library search and package management
for C. I was glad to see clib and CCAN consider coordinating.[3] Hopefully
that effort will eventually bear fruit.

1\. [https://github.com/clibs/clib](https://github.com/clibs/clib) 2\.
[https://ccodearchive.net/](https://ccodearchive.net/) 3\.
[https://github.com/clibs/clib/issues/69](https://github.com/clibs/clib/issues/69)

------
duneroadrunner
Well, since they seem to be including collections of elements, each with
dependency on one or two files, SaferCPlusPlus[1] could be added to the "data
structures" category. It's a collection of safe compatible substitutes for
C/C++'s unsafe elements that's generally intended to be used as a whole, but
certainly supports cherry picking of individual elements. For example, you
could choose to use its safe substitutes for std::vector (and std::array)
while foregoing its safe pointer substitutes.

The implementation design was a trade-off between minimizing redundancy and
minimizing dependencies (and inter-dependencies). Ultimately I decided minimal
dependencies would be more appealing to the user.

[1] shameless plug:
[https://github.com/duneroadrunner/SaferCPlusPlus](https://github.com/duneroadrunner/SaferCPlusPlus)

~~~
zielmicha
There is also _GLIBCXX_DEBUG - if you have defined this symbol, glibc will use
safer variant of std::vector, iterators etc, which asserts when you attempt to
do bad things like reading out of range.

~~~
duneroadrunner
Yup. SaferCPlusPlus has vector and array substitutes that will also catch
"use-after-free" bugs and use of invalid iterators. Clang/LLVM and gcc also
provide "sanitizers" with much more comprehensive checking. But there are
still situations when you'd want to use SaferCPlusPlus [1].

[1]
[https://github.com/duneroadrunner/SaferCPlusPlus#safercplusp...](https://github.com/duneroadrunner/SaferCPlusPlus#safercplusplus-
versus-clangllvm-sanitizers)

------
greyman
I am glad that developers are willing to create truly free software under
public-domain license, so one can just use it at will (for example copy just
one method from the code) without worrying about licenses or needing to hire a
lawyer.

------
logicallee
For anyone wondering why the FAQ says:

>Q. Why isn't SQLite's amalgamated build on this list?

>A. Come on.

For the answer, look at the SQLite Amalgamation page[1]

>1\. Executive Summary

>Over 100 separate source files are concatenated into a single large files of
C-code named "sqlite3.c" and called "the amalgamation". The amalgamation
contains everything an application needs to embed SQLite. The amalgamation
file is more than 180,000 lines long and over 6 megabytes in size.

So although it's technically a single file, at 180 000 lines and 6 Megabytes,
it's not really in the spirit of the page.

[1]
[https://www.sqlite.org/amalgamation.html](https://www.sqlite.org/amalgamation.html)

~~~
liuyanghejerry
This is so funny. BTW, even now, browser still love your JavaScript project in
few large files. And before HTTP2 widely used, I think it's still the way to
go.

------
quinthar
In the FAQ it just says "Come on" for SQLite included in the list. Can you
provide a bit more detail? This feels like a pretty great option to include
here.

~~~
idm
My theory: everybody knows sqlite can be distributed as a single-file library
that you can happily embed. However, it's a pretty complex beast compared to
the rest of the list and maybe it's outside the vision of the list altogether,
which features much simpler projects.

Edit: to be even clearer, sqlite is 100+ .c files. Yes, you can make it one
"amalgamated" .c file, but no developer works on that file; they work on the
100+ smaller .c files.

~~~
sfifs
I guess you mean SQLite developers don't use the amalgamation. It is used by
users.

The amalgamation is used for instance in the most popular Golang library to
provide SQLite engine.

~~~
jbarham
For the record: [https://github.com/mattn/go-
sqlite3](https://github.com/mattn/go-sqlite3)

------
Animats
Good idea, but submissions require forking the project, editing their
directory file, and submitting a pull request. All this does is maintain a
list. I just submitted "algebra3.h", vector functions for 2, 3, and 4 element
vectors, all as inline C++. I've had that on a web site of mine since the
1990s.

~~~
tripzilch
That one seems to be in there now? Or at least in the recent additions
section. It was the first one I clicked on :)

I'm not very familiar with C++ (it's been a long time ago), but a friend
recently came to me with some questions about vector/matrix algebra (though at
first he didn't know his questions went by those names), wanted to write a toy
physics engine in C++. I kind of assumed that C++ would have at least a basic
selection of your typical vec2/3/4 data types and functions in its standard
library--because C++ pretty much invented (or at least made popular, right?)
operator overloading, and I don't know about everybody else, but vector math
would be the very first thing that comes to mind learning about operator
overloading and thinking "huh, what useful thing could I do with this".

But it's not there (unless I didn't look properly?). So I told him (after
explaining why vectors and matrices are nice to have in the first place) to
write his own, because it's not hard if you don't need _all_ the functionality
in algebra3.h and it's a nice exercise for C++ classes and operator
overloading.

Now my questions, since we're in a thread with people that know a whole lot
more about C++ than I do:

Is there really no mathematical vector/matrix algebra data type with
associated functions in the C++ standard libraries? (And I don't mean the one
called Vector, that's really sorta-kinda like a list)

If no, what do people generally use? Lightweight, or big framework?

On the lightweight side of things, is algebra3.h a relatively good choice?

I looked through the code and saw a few things that bugged me. Angles are in
degrees (math code and functions use radians internally _everywhere_ , this is
just burning some useless muls and divs for a unit change that _will_ be
mistaken and cause a bug in someone's code some day somewhere). That one I'd
immediately change before using this lib :) I'd like to reserve degrees just
for displaying and input.

I've been coding a lot of GLSL recently, and IMO their built-in vector math
functions are pretty well thought-out, so there's some choices that differ.
Like the overloaded asterisk (multiply) operator is sometimes a dot product,
other times element-wise mul. From experience, I've found my code to be more
readable when using an asterisk for operations just interchangeable with a
scalar multiply (which is element-wise), 'dot' and 'cross' as names functions
(you don't use the cross-product nearly as much to warrant its own operator,
and the name 'dot' hits the sweet spot between being short enough so it
doesn't matter you use it a bit more often) and preferably another operator
for matrix-multiply (maybe the '@'? is that possible in C++?) to signify it's
not commutative and just really really not at all what the asterisk-operator
usually does with scalars and vectors (bonus, you also get an element-wise
multiply for matrices, yay).

Ooh and here's a nice one, is there any nice way to implement dimension
swizzling of vectors in C++ like GLSL has? Like you could say P.xzyy and get a
vec4 consisting of elements 0,2,1 and 1 of vector P, respectively. Because
that was like the coolest thing ever when first learning GLSL, I've never seen
anything like it :)

~~~
Animats
The design of that library isn't mine. It's from "Graphic Gems IV". I just
rewrote it in C++ as all inline, when I needed it for a physics engine in the
1990s. It's been used in a few college courses.

In general, overloading operators beyond arithmetic is a bad idea. It just
leads to confusion and strange mixed-mode semantics. Python suffers from this.
"+" is concatenate for vectors of numbers, but addition for numpy arrays. You
can add a vector to a numpy array in Python without getting an error. Guess
which action takes place.

------
Xeoncross
I've been wondering where I can find guys to write a few dozen lines of C/C++
for my Go and/or bash scripts.

I often need some simple thing that can take input from Stdin while I'm piping
stuff around in a bash script. Like telling me the db audio level of a mp3
file while reading through all the files in a directory.

~~~
shermanyo
Just a heads up, in case you haven't seen it. Go has pretty nice handling of
pipes for certain use cases. HTH

[http://blog.ralch.com/tutorial/golang-command-line-
pipes/](http://blog.ralch.com/tutorial/golang-command-line-pipes/)

------
cobalt
There's another set of similar libraries called the STB Libraries, primarily
aimed at gamedev/graphicsdev:
[https://github.com/nothings/stb](https://github.com/nothings/stb)

~~~
kLeeIsDead
That's the exact same one as the first line in the OP, if you bothered to
click the link

~~~
TD-Linux
The OP one seems to be different - for example, it doesn't list stb_vorbis.c.

------
NotThe1Pct
I couldnt really find anything useful in there...

------
stevemk14ebr
IMHO it's dumb to limit the libraries to just two files. Whether or not a
library is included in the list should be determined on a per-library basis.
Some header-only libraries simply require more than one header file or the
source code becomes an absolute mess.

~~~
IshKebab
The single-file approach is to simplify including the library in your project.
Just copy the file and type #include "whatever.h".

It's because C/C++ _still_ doesn't have a decent module system, nor a
universally accepted library distribution system. You can still write the
project as separate files and the concatenate them for distribution.

~~~
aninteger
It sorta does. It's just we use the packaging tools of the operating system
(like apt-get and equivalent tools on other systems). That and pkg-config
solves 99% of the problem on Unix-based systems. Windows on the other hand...
Good luck.

------
israrkhan
This is a great list, primarily for learning and experimenting. However I
would not use these for production purpose. Typically production apps require
Security, Performance, handling of edge cases, Memory leaks, and reliability
in addition to functionality. It is easier to implement functionality in a
single file library, however the scope increases significantly when you start
adding things like os/hw/api abstraction layers, and aspects other than
functionality.

~~~
ovao
You've not explained how a header-only approach precludes "security,
performance, handling of edge cases, memory leaks [presumably you mean the
absence of] and reliability".

~~~
cgvgffyv
Leave him be, he must've just learned all those words, and in the great
tradition of average human discourse, put them together to form a
grammatically coherent yet nonsensical utterance that nonetheless achieves its
main objective, namely making him feel good about himself.

(See? I can do it too!)

