Skip to content
Myr

advice for how to make your own stb-style library

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parent 38c36fa · Browse files · 1 file+184 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+184 added−0 removed

  • other:+184 added−0 removed

1 file+184 added−0 removed

added:stb_howto.txt+184 added
1Lessons learned about how to make a header-file library2V1.03September 2013 Sean Barrett45Things to do in an stb-style header-file library,6and rationales:7891. #define LIBRARYNAME_IMPLEMENTATION1011Use a symbol like the above to control creating12the implementation. (I used a far-less-clear name13in my first header-file library; it became14clear that was a mistake once I had multiple15libraries.)1617Include a "header-file" section with header-file18guards and declarations for all the functions,19but don't guard the implementation. That way,20if client's header file X includes your header file for21declarations, they can still include header file X22in the source file that creates the implementation;23if you guard the implementation too, then the first24include (before the #define) creates the declarations,25and the second one (after the #define) does nothing.2627282. AVOID DEPENDENCIES2930Don't rely on anything other than the C standard libraries.3132(If you're creating a library specifically to leverage/wrap33some other library, then obviously you can rely on that34library. But if that library is public domain, you might35be better off directly embedding the source, to reduce36dependencies for your clients. But of course now you have37to update whenever that library updates.)3839If you use stdlib, consider wrapping all stdlib calls in40macros, and then conditionally define those macros to the41stdlib function, allowing the user to replace them.4243For functions with side effects, like memory allocations,44consider letting the user pass in a context and pass45that in to the macros. (The stdlib versions will ignore46the parameter.) Otherwise, users may have to use global47or thread-local variables to achieve the same effect.4849503. AVOID MALLOC5152You can't always do this, but when you can, embedded developers53will appreciate it. I almost never bother avoiding, as it's54too much work (and in some cases is pretty infeasible;55see http://nothings.org/gamedev/font_rendering_malloc.txt ).56But it's definitely something one of the things I've gotten57the most pushback on from potential users.5859604. ALLOW STATIC IMPLEMENTATION6162Have a #define which makes function declarations and63function definitions static. This makes the implementation64private to the source file that creates it. This allows65people to use your library multiple times in their project66without collision. (This is only necessary if your library67has configuration macros or global state, or if your68library has multiple versions that are not backwards69compatible. I've run into both of those cases.)7071725. MAKE ACCESSIBLE FROM C7374Making your code accessible from C instead of C++ (i.e.75either coding in C, or using extern "C") makes it more76straightforward to be used in C and in other languages,77which often only have support for C bindings, not C++.78(One of the earliest results I found in googling for79stb_image was a Haskell wrapper.) Otherwise, people80have to wrap it in another set of function calls, and81the whole point here is to make it convenient for people82to use, isn't it? (See below.)8384I prefer to code entirely in C, so the source file that85instantiates the implementation can be C itself, for86those crazy people out there who are programming in C.87But it's probably not a big hardship for a C programmer88to create a single C++ source file to instantiate your89library.9091926. NAMESPACE PRIVATE FUNCTIONS9394Try to avoid having names in your source code that95will cause conflicts with identical names in client96code. You can do this either by namespacing in C++,97or prefixing with your library name in C.9899In C, generally, I use the same prefix for API100functions and private symbols, such as "stbtt_"101for stb_truetype; but private functions (and102static globals) use a second underscore as103in "stbtt__" to further minimize the chance of104additional collisions in the unlikely but not105impossible event that users write wrapper106functions that have names of the form "stbtt_".107(Consider the user that has used "stbtt_foo"108*successfully*, and then upgrades to a new109version of your library which has a new private110function named either "stbtt_foo" or "stbtt__foo".)111112Note that the double-underscore is reserved for113use by the compiler, but (1) there is nothing114reserved for "middleware", i.e. libraries115desiring to avoid conflicts with user symbols116have no other good options, and (2) in practice117no compilers use double-underscore in the middle118rather than the beginning/end. (Unfortunately,119there is at least one videogame-console compiler that120will warn about double-underscores by default.)1211221237. EASY-TO-COMPLY LICENSE124125I make my libraries public domain. You don't have to.126But my goal in releasing stb-style libraries is to127reduce friction for potential users as much as128possible. That means:129130 a. easy to build (what this file is mostly about)131 b. easy to invoke (which requires good API design)132 c. easy to deploy (which is about licensing)133134I choose to place all my libraries in the public135domain, abjuring copyright, rather than license136the libraries. This has some benefits and some137drawbacks.138139Any license which is "viral" to modifications140causes worries for lawyers, even if their programmers141aren't modifying it.142143Any license which requires crediting in documentation144adds friction which can add up. Valve used to have145a page with a list of all of these on their web site,146and it was insane, and obviously nobody ever looked147at it so why would you care whether your credit appeared148there?149150Permissive licenses like zlib and BSD license are151perfectly reasonable, but they are very wordy and152have only two benefits over public domain: legally-mandated153attribution and liability-control. I do not believe these154are worth the excessive verbosity and user-unfriendliness155these licenses induce, especially in the single-file156case where those licenses tend to be at the top of157the file, the first thing you see. (To the specific158points, I have had no trouble receiving attribution159for my libraries; liability in the face of no explicit160disclaimer of liability is an open question.)161162However, public domain has frictions of its own, because163public domain declarations aren't necessary recognized164in the USA and some other locations. For that reason,165I recommend a declaration along these lines:166167// This software is in the public domain. Where that dedication is not168// recognized, you are granted a perpetual, irrevocable license to copy169// and modify this file as you see fit.170171I typically place this declaration at the end of the initial172comment block of the file and just say 'public domain'173at the top.174175I have had people say they couldn't use one of my176libraries because it was only "public domain" and didn't177have the additional fallback clause, who asked if178I could dual-license it under a traditional license.179180My answer: they can create a derivative work by181modifying one character, and then license that however182they like. (Indeed, *adding* the zlib or BSD license183would be such a modification!) Unfortunately, their184lawyers reportedly didn't like that answer. :(