stb_howto.txt
This file
The walk stopped after 200 commits: lines older than aeec66c point at it.
Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1–185why, one step back1Lessons 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 only guard the implementation with LIBRARYNAME_IMPLEMENTATION,20not the header-file guard. That way, if client's21header file X includes your header file for22declarations, they can still include header file X23in the source file that creates the implementation;24if you guard the implementation too, then the first25include (before the #define) creates the declarations,26and the second one (after the #define) does nothing.2728292. AVOID DEPENDENCIES3031Don't rely on anything other than the C standard libraries.3233(If you're creating a library specifically to leverage/wrap34some other library, then obviously you can rely on that35library. But if that library is public domain, you might36be better off directly embedding the source, to reduce37dependencies for your clients. But of course now you have38to update whenever that library updates.)3940If you use stdlib, consider wrapping all stdlib calls in41macros, and then conditionally define those macros to the42stdlib function, allowing the user to replace them.4344For functions with side effects, like memory allocations,45consider letting the user pass in a context and pass46that in to the macros. (The stdlib versions will ignore47the parameter.) Otherwise, users may have to use global48or thread-local variables to achieve the same effect.4950513. AVOID MALLOC5253You can't always do this, but when you can, embedded developers54will appreciate it. I almost never bother avoiding, as it's55too much work (and in some cases is pretty infeasible;56see http://nothings.org/gamedev/font_rendering_malloc.txt ).57But it's definitely something one of the things I've gotten58the most pushback on from potential users.5960614. ALLOW STATIC IMPLEMENTATION6263Have a #define which makes function declarations and64function definitions static. This makes the implementation65private to the source file that creates it. This allows66people to use your library multiple times in their project67without collision. (This is only necessary if your library68has configuration macros or global state, or if your69library has multiple versions that are not backwards70compatible. I've run into both of those cases.)7172735. MAKE ACCESSIBLE FROM C7475Making your code accessible from C instead of C++ (i.e.76either coding in C, or using extern "C") makes it more77straightforward to be used in C and in other languages,78which often only have support for C bindings, not C++.79(One of the earliest results I found in googling for80stb_image was a Haskell wrapper.) Otherwise, people81have to wrap it in another set of function calls, and82the whole point here is to make it convenient for people83to use, isn't it? (See below.)8485I prefer to code entirely in C, so the source file that86instantiates the implementation can be C itself, for87those crazy people out there who are programming in C.88But it's probably not a big hardship for a C programmer89to create a single C++ source file to instantiate your90library.9192936. NAMESPACE PRIVATE FUNCTIONS9495Try to avoid having names in your source code that96will cause conflicts with identical names in client97code. You can do this either by namespacing in C++,98or prefixing with your library name in C.99100In C, generally, I use the same prefix for API101functions and private symbols, such as "stbtt_"102for stb_truetype; but private functions (and103static globals) use a second underscore as104in "stbtt__" to further minimize the chance of105additional collisions in the unlikely but not106impossible event that users write wrapper107functions that have names of the form "stbtt_".108(Consider the user that has used "stbtt_foo"109*successfully*, and then upgrades to a new110version of your library which has a new private111function named either "stbtt_foo" or "stbtt__foo".)112113Note that the double-underscore is reserved for114use by the compiler, but (1) there is nothing115reserved for "middleware", i.e. libraries116desiring to avoid conflicts with user symbols117have no other good options, and (2) in practice118no compilers use double-underscore in the middle119rather than the beginning/end. (Unfortunately,120there is at least one videogame-console compiler that121will warn about double-underscores by default.)1221231247. EASY-TO-COMPLY LICENSE125126I make my libraries public domain. You don't have to.127But my goal in releasing stb-style libraries is to128reduce friction for potential users as much as129possible. That means:130131 a. easy to build (what this file is mostly about)132 b. easy to invoke (which requires good API design)133 c. easy to deploy (which is about licensing)134135I choose to place all my libraries in the public136domain, abjuring copyright, rather than license137the libraries. This has some benefits and some138drawbacks.139140Any license which is "viral" to modifications141causes worries for lawyers, even if their programmers142aren't modifying it.143144Any license which requires crediting in documentation145adds friction which can add up. Valve used to have146a page with a list of all of these on their web site,147and it was insane, and obviously nobody ever looked148at it so why would you care whether your credit appeared149there?150151Permissive licenses like zlib and BSD license are152perfectly reasonable, but they are very wordy and153have only two benefits over public domain: legally-mandated154attribution and liability-control. I do not believe these155are worth the excessive verbosity and user-unfriendliness156these licenses induce, especially in the single-file157case where those licenses tend to be at the top of158the file, the first thing you see. (To the specific159points, I have had no trouble receiving attribution160for my libraries; liability in the face of no explicit161disclaimer of liability is an open question.)162163However, public domain has frictions of its own, because164public domain declarations aren't necessary recognized165in the USA and some other locations. For that reason,166I recommend a declaration along these lines:167168// This software is dual-licensed to the public domain and under the following169// license: you are granted a perpetual, irrevocable license to copy, modify,170// publish, and distribute this file as you see fit.171172I typically place this declaration at the end of the initial173comment block of the file and just say 'public domain'174at the top.175176I have had people say they couldn't use one of my177libraries because it was only "public domain" and didn't178have the additional fallback clause, who asked if179I could dual-license it under a traditional license.180181My answer: they can create a derivative work by182modifying one character, and then license that however183they like. (Indeed, *adding* the zlib or BSD license184would be such a modification!) Unfortunately, their185lawyers reportedly didn't like that answer. :(