13 ms·
Single-file public-domain/open source C libraries with minimal dependencies
- wellwatch 11y agoWhy isn't SQLite's amalgamated build on this list? Come on. I don't understand this. Can someone explain it to me?
- zyxley 11y ago> The amalgamation is a single C code file, named "sqlite3.c", that contains all C code for the core SQLite library and the FTS3, FTS5, RTREE, DBSTAT, JSON1, and RBU extensions. This file contains about 184K lines of code (113K if you omit blank lines and comments) and is over 6.4 megabytes in size. Though the the various extensions are included in the "sqlite3.c" amalgamation file, they are disabled using #ifdef statements. https://www.sqlite.org/amalgamation.html https://www.sqlite.org/amalgamation.html
- userbinator 11y agoIn other words, it's stretching the definition of "single file" a bit... but then again, I don't know any other SQL databases anywhere near as small as SQLite.
- srpeck 11y agoSmallest commercially viable SQL database is probably the latest k5/kdb (http://kparc.com/ http://kparc.com/), which is implemented in something like 9KB of code (source: http://kparc.com/o.htm http://kparc.com/o.htm).
- DenisM 11y agoYeah I think that's rather silly, both the lack of an explanation and the implied reason if we guessed it correctly (thanks for making us guess and have this hypothetical discussion). Being single file is about simplified project management. How is the file size relevant?
- morebetterer 11y agoWhat about: * sqlite3 amalgamation: https://www.sqlite.org/download.html https://www.sqlite.org/download.html * duktape javascript interpreter: http://duktape.org http://duktape.org
- greenyoda 11y agoHere are some more... Kazlib contains a few single-file (actually one .c file and one .h file) open source C libraries. For example, there's a dictionary data structure based on red-black trees, dict.c/dict.h, which I've found to be useful and reliable. There's also a linked list library, a hash table library, etc., but I haven't used these. Home page: http://www.kylheku.com/~kaz/kazlib.html http://www.kylheku.com/~kaz/kazlib.html Source code: http://git.savannah.gnu.org/cgit/kazlib.git/tree/ http://git.savannah.gnu.org/cgit/kazlib.git/tree/ The latest version uses the BSD License.
- rwmj 11y agohttp://git.annexia.org/?p=miniexpect.git http://git.annexia.org/?p=miniexpect.git It's an "expect"-like library written in C (and more sane than the Tcl original). It's 3 files, because it has a header file and a manual page. It's just 390 lines of code because it's implemented using some PCRE trickery.
- chubot 11y agoUh, this stb library [1] seems to follow a horrible pattern: not only combining your code into a single .c file (which is fine, like the sqlite amalgamation), but putting the code into a single .h file. [2] I've never seen a C or C++ codebase that does this. Maybe you can rely on link-time deduplication, but it will still cause duplicate compilation, and thus increased compile times. I haven't thought it about it lately, but I thought the common wisdom was not to depend on the linker to dedupe or strip unused symbols. Actually it should cause link-time errors, not just duplicated cause. The justification is also somewhat ridiculous: Why single-file headers? Windows doesn't have standard directories where libraries live. That makes deploying libraries in Windows a lot more painful than open source developers on Unix-derivates generally realize. (It also makes library dependencies a lot worse in Windows.) Really? Why not just lay out your source normally, and then munge it with a simple script into something Windows can handle (similar to sqlite)? Because writing trivial scripts on Windows is also a pain? [1] https://github.com/nothings/stb https://github.com/nothings/stb [2] https://github.com/nothings/stb/blob/master/stb_c_lexer.h https://github.com/nothings/stb/blob/master/stb_c_lexer.h
- mbrock 11y agoNote that the implementation code is conditionally compiled. By default you only compile the headers, so it's up to you instantiate the implementation somewhere by setting a preprocessor symbol before you include the library. This is described in the comment at the top of the file.
- chubot 11y agoOK, I missed that... it makes it a slightly less crazy pattern, but still weird and offputting. If you really have problems adding one .h and one .c file to your project, I think the development environment is broken. (Admittedly, I last used Visual Studio C++ in 2007... I installed it from a DVD, only to find it was broken, and I had to copy a .DLL from the Microsoft website to get it to work.)
- lifthrasiir 11y agoC programmers certainly hate dependencies. So much that the most extreme class cannot even tolerate multi-file external dependencies. That is really extreme, but given the history of dealing (or coping) with dependencies such paranoia is rather justified. > Why not just lay out your source normally, and then munge it with a simple script into something Windows can handle (similar to sqlite)? Because writing trivial scripts on Windows is also a pain? Instead of making a big library with multiple files, they took an alternative to make a small well-defined library with a single file and small number of entry points.
- yorhel 11y agoShameless plug: I also maintain a few small single-file C libs at https://dev.yorhel.nl/ylib https://dev.yorhel.nl/ylib And, separately, a small-single-file-but-correct XML parser: https://dev.yorhel.nl/yxml https://dev.yorhel.nl/yxml
- deleted 11y ago[deleted]
- jarcane 11y agoFor parsing, there's the excellent mpc, a parser combinator library in highly portable C. https://github.com/orangeduck/mpc https://github.com/orangeduck/mpc I even managed to use it drop in for a RiscOS project once.
- kgabis 11y agoShameless plug: https://github.com/kgabis/parson https://github.com/kgabis/parson (2-file (.h and .c) json parser)
- djcapelis 11y agoJust proposed one of my projects for inclusion: https://github.com/djcapelis/atomic-ring https://github.com/djcapelis/atomic-ring Lock-free Single Producer, Single Consumer (SPSC) queue. The dependencies include C11 and that's it. No POSIX required, should work on any arch you can find a C11 compiler for.
- biokoda 11y agoIf all you're doing is passing pointers from one thread to another, lock free is not a very good choice. Try a trivial spinlock based implementation. It will outperform it easily.
- djcapelis 11y agoI strongly doubt you're going to build a faster thread-safe workqueue by adding spinlocks to it, but if you think what I wrote is too slow, feel free to build something faster and show the speed difference in a benchmark. You can use the code in test/ if you'd like. The code I wrote does leave an optimization or two on the table. Apple's lockfree queue in GCD is faster than mine because of batching. It also has a ton more features, including being (IIRC) MPSC.
- biokoda 11y agoLocking is cheap, switching a pointer around is cheap. The only expensive thing is waking up a thread that is waiting on a lock. Spinlocks avoid that problem, which is why they outperform other implementations in terms of throughput (of course inversely proportional to the amount of work you do during a lock). Atomic operations have a fixed cost. Spinlocks only really have a cost when contention is high. When you're just moving a pointer to another place, you're not likely to have that much contention to make atomic operations worth it.
- djcapelis 11y agoI don't think you've got this quite right. Spinlocks require memory barriers. C11 atomics are, among other things, a standardized way to implement cross platform memory barriers without dropping into platform specific assembly.
- rat87 11y agoPlug for a tiny header library I wrote https://github.com/rtaycher/debug_print_h https://github.com/rtaycher/debug_print_h It's a little thing for simple printf debugging. I got tired as hell of using formatting strings. It prints line, file, and function, expression and value. Since it stands out /greppable it's easier to delete then random printfs. It has support for different colors(to make things stand out) and uses c99 _Generic to support different types with a single "function" and c initializers for named arguments. It supports basic c types and you can sort of extend it by modyfing the header(unfortunately I couldn't find anything cleaner using c). C99 only (works on gcc>4.9 (due to use of designated initializers) and llvm/clang >3.3 (maybe earlier but I tested it with 3.3) I was hoping to support msvc but it looks like Microsoft hasn't implemented _Generic yet.
- kyberias 11y agoBut... isn't _Generic part of C11, not C99? You actually require C11. Big difference.
- rat87 11y agoOops. You're absolutely right it's C11.
- kyberias 11y agoYeah, I wondered about that because I thought VC's C99 implementation is _relatively_ useful but again C11 might never arrive there.
- rat87 11y agoLooks I'm wrong again, it's been a while since I wrote it it also uses typeof gcc/llvm extension, there might be a way to fix that but since msvc lacks _Generic I didn't bother. I was also hoping to support c++ but c++ doesn't do designated initializers.
- Sir_Cmpwn 11y agoMostly related: https://github.com/blanham/PDCLib https://github.com/blanham/PDCLib Not one file but public domain and a great project :)
- Tim61 11y agoJust proposed two of my C projects for inclusion: TinyExpr - evaluate math from string - https://github.com/codeplea/tinyexpr https://github.com/codeplea/tinyexpr minctest - very minimal C unit tests - https://github.com/codeplea/minctest https://github.com/codeplea/minctest TinyExpr is a single source file + header, while minctest is a single header. Both are zlib licensed. Also, I'd appreciate feedback if anyone is interested.
- arcticgeek 11y agoAnother good source of small utility libraries is CCAN. List of available modules: https://ccodearchive.net/list.html https://ccodearchive.net/list.html