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
Provenance
- Commit
d4f114a- Tree
5298345- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
38c36fa
1 file changed+184 added−0 removed
added:stb_howto.txt+184 added
1+Lessons learned about how to make a header-file library2+V1.03+September 2013 Sean Barrett4+5+Things to do in an stb-style header-file library,6+and rationales:7+8+9+1. #define LIBRARYNAME_IMPLEMENTATION10+11+Use a symbol like the above to control creating12+the implementation. (I used a far-less-clear name13+in my first header-file library; it became14+clear that was a mistake once I had multiple15+libraries.)16+17+Include a "header-file" section with header-file18+guards and declarations for all the functions,19+but don't guard the implementation. That way,20+if client's header file X includes your header file for21+declarations, they can still include header file X22+in the source file that creates the implementation;23+if you guard the implementation too, then the first24+include (before the #define) creates the declarations,25+and the second one (after the #define) does nothing.26+27+28+2. AVOID DEPENDENCIES29+30+Don't rely on anything other than the C standard libraries.31+32+(If you're creating a library specifically to leverage/wrap33+some other library, then obviously you can rely on that34+library. But if that library is public domain, you might35+be better off directly embedding the source, to reduce36+dependencies for your clients. But of course now you have37+to update whenever that library updates.)38+39+If you use stdlib, consider wrapping all stdlib calls in40+macros, and then conditionally define those macros to the41+stdlib function, allowing the user to replace them.42+43+For functions with side effects, like memory allocations,44+consider letting the user pass in a context and pass45+that in to the macros. (The stdlib versions will ignore46+the parameter.) Otherwise, users may have to use global47+or thread-local variables to achieve the same effect.48+49+50+3. AVOID MALLOC51+52+You can't always do this, but when you can, embedded developers53+will appreciate it. I almost never bother avoiding, as it's54+too much work (and in some cases is pretty infeasible;55+see http://nothings.org/gamedev/font_rendering_malloc.txt ).56+But it's definitely something one of the things I've gotten57+the most pushback on from potential users.58+59+60+4. ALLOW STATIC IMPLEMENTATION61+62+Have a #define which makes function declarations and63+function definitions static. This makes the implementation64+private to the source file that creates it. This allows65+people to use your library multiple times in their project66+without collision. (This is only necessary if your library67+has configuration macros or global state, or if your68+library has multiple versions that are not backwards69+compatible. I've run into both of those cases.)70+71+72+5. MAKE ACCESSIBLE FROM C73+74+Making your code accessible from C instead of C++ (i.e.75+either coding in C, or using extern "C") makes it more76+straightforward to be used in C and in other languages,77+which often only have support for C bindings, not C++.78+(One of the earliest results I found in googling for79+stb_image was a Haskell wrapper.) Otherwise, people80+have to wrap it in another set of function calls, and81+the whole point here is to make it convenient for people82+to use, isn't it? (See below.)83+84+I prefer to code entirely in C, so the source file that85+instantiates the implementation can be C itself, for86+those crazy people out there who are programming in C.87+But it's probably not a big hardship for a C programmer88+to create a single C++ source file to instantiate your89+library.90+91+92+6. NAMESPACE PRIVATE FUNCTIONS93+94+Try to avoid having names in your source code that95+will cause conflicts with identical names in client96+code. You can do this either by namespacing in C++,97+or prefixing with your library name in C.98+99+In C, generally, I use the same prefix for API100+functions and private symbols, such as "stbtt_"101+for stb_truetype; but private functions (and102+static globals) use a second underscore as103+in "stbtt__" to further minimize the chance of104+additional collisions in the unlikely but not105+impossible event that users write wrapper106+functions that have names of the form "stbtt_".107+(Consider the user that has used "stbtt_foo"108+*successfully*, and then upgrades to a new109+version of your library which has a new private110+function named either "stbtt_foo" or "stbtt__foo".)111+112+Note that the double-underscore is reserved for113+use by the compiler, but (1) there is nothing114+reserved for "middleware", i.e. libraries115+desiring to avoid conflicts with user symbols116+have no other good options, and (2) in practice117+no compilers use double-underscore in the middle118+rather than the beginning/end. (Unfortunately,119+there is at least one videogame-console compiler that120+will warn about double-underscores by default.)121+122+123+7. EASY-TO-COMPLY LICENSE124+125+I make my libraries public domain. You don't have to.126+But my goal in releasing stb-style libraries is to127+reduce friction for potential users as much as128+possible. That means:129+130+ 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)133+134+I choose to place all my libraries in the public135+domain, abjuring copyright, rather than license136+the libraries. This has some benefits and some137+drawbacks.138+139+Any license which is "viral" to modifications140+causes worries for lawyers, even if their programmers141+aren't modifying it.142+143+Any license which requires crediting in documentation144+adds friction which can add up. Valve used to have145+a page with a list of all of these on their web site,146+and it was insane, and obviously nobody ever looked147+at it so why would you care whether your credit appeared148+there?149+150+Permissive licenses like zlib and BSD license are151+perfectly reasonable, but they are very wordy and152+have only two benefits over public domain: legally-mandated153+attribution and liability-control. I do not believe these154+are worth the excessive verbosity and user-unfriendliness155+these licenses induce, especially in the single-file156+case where those licenses tend to be at the top of157+the file, the first thing you see. (To the specific158+points, I have had no trouble receiving attribution159+for my libraries; liability in the face of no explicit160+disclaimer of liability is an open question.)161+162+However, public domain has frictions of its own, because163+public domain declarations aren't necessary recognized164+in the USA and some other locations. For that reason,165+I recommend a declaration along these lines:166+167+// 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.170+171+I typically place this declaration at the end of the initial172+comment block of the file and just say 'public domain'173+at the top.174+175+I have had people say they couldn't use one of my176+libraries because it was only "public domain" and didn't177+have the additional fallback clause, who asked if178+I could dual-license it under a traditional license.179+180+My answer: they can create a derivative work by181+modifying one character, and then license that however182+they like. (Indeed, *adding* the zlib or BSD license183+would be such a modification!) Unfortunately, their184+lawyers reportedly didn't like that answer. :(