I'm looking at a header-only C "library": https://github.com/zserge/jsmn/blob/master/jsmn.h
As far as I can understand, this code will be compiled into every object file where the .c file includes jsmn.h
, wasting space.
(The file's function definitions are inside #ifndef JSMN_HEADER
, so you could use this as a "traditional" header file by defining JSMN_HEADER.)
- Why hasn't it been written as a "traditional"
.c
and.h
pair? - Is the linker clever enough to dedup function identical definitions between object files? I would have expected "duplicate symbol" errors.
- What advantage does putting code in headers give in C? (Not C++.)
- From where do you get the function definitions if you use
#define JSMN_HEADER
before importing? - Is
jsmn.h
being header-only a clever trick, from which I can learn?